home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / g++-dist / cplus-search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-22  |  86.0 KB  |  3,153 lines

  1. /* Breadth-first and depth-first routines for
  2.    searching multiple-inheritance lattice for GNU C++.
  3.    Copyright (C) 1987 Free Software Foundation, Inc.
  4.    Contributed by Michael Tiemann (tiemann@mcc.com)
  5.  
  6. This file is part of GNU CC.
  7.    
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* High-level class interface. */
  24.  
  25. #include "config.h"
  26. #include "tree.h"
  27. #include "cplus-tree.h"
  28. #include "obstack.h"
  29. #include "flags.h"
  30. #include "assert.h"
  31. #include <stdio.h>
  32.  
  33. /* For expand_asm_operands.  */
  34. extern char *input_filename;
  35. extern int lineno;
  36.  
  37. #define NULL 0
  38.  
  39. #define obstack_chunk_alloc xmalloc
  40. #define obstack_chunk_free free
  41.  
  42. extern int xmalloc ();
  43. extern void free ();
  44.  
  45. void init_search ();
  46. extern struct obstack *current_obstack;
  47.  
  48. #include "stack.h"
  49.  
  50. /* Obstack used for remembering decision points of breadth-first.  */
  51. static struct obstack search_obstack;
  52.  
  53. /* Obstack used to bridge from one function context to another.  */
  54. static struct obstack bridge_obstack;
  55.  
  56. /* Methods for pushing and popping objects to and from obstacks.  */
  57.  
  58. struct stack_level *
  59. push_stack_level (obstack, tp, size)
  60.      struct obstack *obstack;
  61.      void *tp;
  62.      int size;
  63. {
  64.   struct stack_level *stack;
  65.   stack = (struct stack_level *) obstack_next_free (obstack);
  66.   obstack_grow (obstack, tp, size);
  67.   obstack_finish (obstack);
  68.   stack->obstack = obstack;
  69.   stack->first = (tree *) obstack_base (obstack);
  70.   stack->limit = obstack_room (obstack) / sizeof (tree *);
  71.   return stack;
  72. }
  73.  
  74. struct stack_level *
  75. pop_stack_level (stack)
  76.      struct stack_level *stack;
  77. {
  78.   struct stack_level *tem = stack;
  79.   struct obstack *obstack = tem->obstack;
  80.   stack = tem->prev;
  81.   obstack_free (obstack, tem);
  82.   return stack;
  83. }
  84.  
  85. #define search_level stack_level
  86. static struct search_level *search_stack;
  87.  
  88. static tree lookup_field_1 ();
  89. static int lookup_fnfields_1 ();
  90.  
  91. /* Allocate a level of searching.  */
  92. static struct search_level *
  93. push_search_level (stack, obstack)
  94.      struct stack_level *stack;
  95.      struct obstack *obstack;
  96. {
  97.   struct search_level tem;
  98.   tem.prev = stack;
  99.  
  100.   return push_stack_level (obstack, &tem, sizeof (tem));
  101. }
  102.  
  103. /* Discard a level of search allocation.  */
  104. #define pop_search_level pop_stack_level
  105.  
  106. /* Search memoization.  */
  107. struct type_level
  108. {
  109.   struct stack_level base;
  110.  
  111.   /* First object allocated in obstack of entries.  */
  112.   char *entries;
  113.  
  114.   /* Number of types memoized in this context.  */
  115.   int len;
  116.  
  117.   /* Type being memoized; save this if we are saving
  118.      memoized contexts.  */
  119.   tree type;
  120. };
  121.  
  122. /* Obstack used for memoizing member and member function lookup.  */
  123.  
  124. static struct obstack type_obstack, type_obstack_entries;
  125. static struct type_level *type_stack;
  126. static tree _vptr_name;
  127.  
  128. /* Make things that look like tree nodes, but allocate them
  129.    on type_obstack_entries.  */
  130. static int my_tree_node_counter;
  131. static tree my_tree_cons (), my_build_string ();
  132.  
  133. extern int flag_memoize_lookups, flag_save_memoized_contexts;
  134.  
  135. /* Variables for gathering statistics.  */
  136. static int my_memoized_entry_counter;
  137. static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2];
  138. static int memoized_fields_searched[2];
  139. static int n_fields_searched;
  140. static int n_calls_lookup_field, n_calls_lookup_field_1;
  141. static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
  142. static int n_calls_get_base_type;
  143. static int n_outer_fields_searched;
  144. static int n_contexts_saved;
  145.  
  146. /* Local variables to help save memoization contexts.  */
  147. static tree prev_type_memoized;
  148. static struct type_level *prev_type_stack;
  149.  
  150. /* Allocate a level of type memoziation context.  */
  151. static struct type_level *
  152. push_type_level (stack, obstack)
  153.      struct stack_level *stack;
  154.      struct obstack *obstack;
  155. {
  156.   struct type_level tem;
  157.  
  158.   tem.base.prev = stack;
  159.  
  160.   obstack_finish (&type_obstack_entries);
  161.   tem.entries = (char *) obstack_base (&type_obstack_entries);
  162.   tem.len = 0;
  163.   tem.type = NULL_TREE;
  164.  
  165.   return (struct type_level *)push_stack_level (obstack, &tem, sizeof (tem));
  166. }
  167.  
  168. /* Discard a level of type memoziation context.  */
  169.  
  170. static struct type_level *
  171. pop_type_level (stack)
  172.      struct type_level *stack;
  173. {
  174.   obstack_free (&type_obstack_entries, stack->entries);
  175.   return (struct type_level *)pop_stack_level (stack);
  176. }
  177.  
  178. /* Make something that looks like a TREE_LIST, but
  179.    do it on the type_obstack_entries obstack.  */
  180. static tree
  181. my_tree_cons (purpose, value, chain)
  182.      tree purpose, value, chain;
  183. {
  184.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list));
  185.   TREE_UID (p) = ++my_tree_node_counter;
  186.   TREE_TYPE (p) = 0;
  187.   ((int *)p)[3] = 0;
  188.   TREE_SET_CODE (p, TREE_LIST);
  189.   TREE_PURPOSE (p) = purpose;
  190.   TREE_VALUE (p) = value;
  191.   TREE_CHAIN (p) = chain;
  192.   return p;
  193. }
  194.  
  195. static tree
  196. my_build_string (str)
  197.      char *str;
  198. {
  199.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string));
  200.   TREE_UID (p) = ++my_tree_node_counter;
  201.   TREE_TYPE (p) = 0;
  202.   ((int *)p)[3] = 0;
  203.   TREE_SET_CODE (p, STRING_CST);
  204.   TREE_STRING_POINTER (p) = str;
  205.   TREE_STRING_LENGTH (p) = strlen (str);
  206.   return p;
  207. }
  208.  
  209. static tree
  210. my_copy_node (node)
  211.      tree node;
  212. {
  213.   struct obstack *ambient_obstack = current_obstack;
  214.   tree t;
  215.  
  216.   current_obstack = &type_obstack_entries;
  217.  
  218.   t = copy_node (node);
  219.   TREE_UID (t) = ++my_tree_node_counter;
  220.  
  221.   current_obstack = ambient_obstack;
  222.   return t;
  223. }
  224.  
  225. /* Memoizing machinery to make searches for multiple inheritance
  226.    reasonably efficient.  */
  227. #define MEMOIZE_HASHSIZE 8
  228. typedef struct memoized_entry
  229. {
  230.   struct memoized_entry *chain;
  231.   int uid;
  232.   tree data_members[MEMOIZE_HASHSIZE];
  233.   tree function_members[MEMOIZE_HASHSIZE];
  234. } *ME;
  235.  
  236. #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain)
  237. #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid)
  238. #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX])
  239. #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX])
  240. #define MEMOIZED_HASH_FN(NODE) (TREE_UID (NODE)&(MEMOIZE_HASHSIZE - 1))
  241.  
  242. static struct memoized_entry *
  243. my_new_memoized_entry (chain)
  244.      struct memoized_entry *chain;
  245. {
  246.   struct memoized_entry *p =
  247.     (struct memoized_entry *)obstack_alloc (&type_obstack_entries,
  248.                         sizeof (struct memoized_entry));
  249.   bzero (p, sizeof (struct memoized_entry));
  250.   MEMOIZED_CHAIN (p) = chain;
  251.   MEMOIZED_UID (p) = ++my_memoized_entry_counter;
  252.   return p;
  253. }
  254.  
  255. /* When a new function or class context is entered, we build
  256.    a table of types which have been searched for members.
  257.    The table is an array (obstack) of types.  When a type is
  258.    entered into the obstack, its CLASSTYPE_MTABLE_ENTRY
  259.    field is set to point to a new record, of type struct memoized_entry.
  260.  
  261.    A non-NULL TREE_TYPE of the entry contains a visibility error message.
  262.  
  263.    The slots for the data members are arrays of tree nodes.
  264.    These tree nodes are lists, with the TREE_PURPOSE
  265.    of this list the known member name, and the TREE_VALUE
  266.    as the FIELD_DECL for the member.
  267.  
  268.    For member functions, the TREE_PURPOSE is again the
  269.    name of the member functions for that class,
  270.    and the TREE_VALUE of the list is a pairs
  271.    whose TREE_PURPOSE is a member functions of this name,
  272.    and whose TREE_VALUE is a list of known argument lists this
  273.    member function has been called with.  The TREE_TYPE of the pair,
  274.    if non-NULL, is an error message to print.  */
  275.  
  276. /* Tell search machinery that we are entering a new context, and
  277.    to update tables appropriately.
  278.  
  279.    TYPE is the type of the context we are entering, which can
  280.    be NULL_TREE if we are not in a class's scope.
  281.  
  282.    USE_OLD, if nonzero tries to use previous context.  */
  283. void
  284. push_memoized_context (type, use_old)
  285.      tree type;
  286.      int use_old;
  287. {
  288.   int len;
  289.   tree *tem;
  290.  
  291.   if (prev_type_stack)
  292.     {
  293.       if (use_old && prev_type_memoized == type)
  294.     {
  295. #ifdef GATHER_STATISTICS
  296.       n_contexts_saved++;
  297. #endif
  298.       type_stack = prev_type_stack;
  299.       prev_type_stack = 0;
  300.  
  301.       tem = &type_stack->base.first[0];
  302.       len = type_stack->len;
  303.       while (len--)
  304.         CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = tem[len*2+1];
  305.       return;
  306.     }
  307.       /* Otherwise, need to pop old stack here.  */
  308.       type_stack = pop_type_level (prev_type_stack);
  309.       prev_type_memoized = 0;
  310.       prev_type_stack = 0;
  311.     }
  312.  
  313.   type_stack = push_type_level (type_stack, &type_obstack);
  314.   type_stack->type = type;
  315. }
  316.  
  317. /* Tell search machinery that we have left a context.
  318.    We do not currently save these contexts for later use.
  319.    If we wanted to, we could not use pop_search_level, since
  320.    poping that level allows the data we have collected to
  321.    be clobbered; a stack of obstacks would be needed.  */
  322. pop_memoized_context (use_old)
  323.      int use_old;
  324. {
  325.   int len;
  326.   tree *tem = &type_stack->base.first[0];
  327.  
  328.   if (! flag_save_memoized_contexts)
  329.     use_old = 0;
  330.   else if (use_old)
  331.     {
  332.       len = type_stack->len;
  333.       while (len--)
  334.     tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]);
  335.  
  336.       prev_type_stack = type_stack;
  337.       prev_type_memoized = type_stack->type;
  338.     }
  339.  
  340.   if (flag_memoize_lookups)
  341.     {
  342.       len = type_stack->len;
  343.       while (len--)
  344.     CLASSTYPE_MTABLE_ENTRY (tem[len*2])
  345.       = MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2]));
  346.     }
  347.   if (! use_old)
  348.     type_stack = pop_type_level (type_stack);
  349.   else
  350.     type_stack = (struct type_level *)type_stack->base.prev;
  351. }
  352.  
  353. /* Some simple list processing predicates.  */
  354.  
  355. /* Check whether TYPE is immediately derived from PARENT.
  356.    Return actual base information if so.  Otherwise, return 0.  */
  357. tree
  358. get_base_type_1 (parent, type)
  359.      register tree parent, type;
  360. {
  361.   register int i;
  362.  
  363.   parent = TYPE_MAIN_VARIANT (parent);
  364.   type = TYPE_MAIN_VARIANT (type);
  365.  
  366.   for (i = 1; i <= CLASSTYPE_N_BASECLASSES (type); i++)
  367.     if (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (type, i)) == parent)
  368.       return CLASSTYPE_BASECLASS (type, i);
  369.  
  370.   return 0;
  371. }
  372.  
  373. /* Check whether TYPE is derived from PARENT.
  374.    Return the actual base information if so.  Otherwise return 0.
  375.    If PROTECT is 1, then emit an error message if access to
  376.    a public field of PARENT would be private.
  377.    If PROTECT is 2, then emit an error message if
  378.    TYPE is derived from PARENT via private visibility rules.
  379.    If PROTECT is 3, then immediately private baseclass is ok,
  380.    but deeper than that, if private, emit error message.  */
  381. tree
  382. get_base_type (parent, type, protect)
  383.      register tree parent, type;
  384. {
  385.   tree xtype = type;
  386.   tree otype;
  387.   int head = 0, tail = 0;
  388.   int is_private = 0;
  389.   tree rval = NULL_TREE;
  390.   int rval_private = 0;
  391.   tree friends = current_class_type
  392.     ? CLASSTYPE_FRIEND_CLASSES (type) : NULL_TREE;
  393.  
  394. #ifdef GATHER_STATISTICS
  395.   n_calls_get_base_type++;
  396. #endif
  397.  
  398.   parent = TYPE_MAIN_VARIANT (parent);
  399.   search_stack = push_search_level (search_stack, &search_obstack);
  400.  
  401.   while (1)
  402.     {
  403.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  404.  
  405.       /* Process and/or queue base types.  */
  406.       for (i = 1; i <= n_baselinks; i++)
  407.     if (CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) == 0)
  408.       {
  409.         int via_private = is_private || !CLASSTYPE_VIA_PUBLIC (type, i);
  410.  
  411.         if (via_private == 0)
  412.           ;
  413.         else if (protect == 0)
  414.           via_private = 0;
  415.         else if (protect == 1 && type == current_class_type)
  416.           /* The immediate base class of the class we are in
  417.          does let its public members through.  */
  418.           via_private = 0;
  419. #ifndef NOJJG
  420.         else if (protect
  421.              && friends != NULL_TREE
  422.              && type == xtype
  423.              && value_member (current_class_type, friends))
  424.           /* Friend types of the most derived type have access
  425.          to its baseclass pointers.  */
  426.           via_private = 0;
  427. #endif
  428.  
  429.         CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) = 1;
  430.         otype = type;
  431.         obstack_ptr_grow (&search_obstack, CLASSTYPE_BASECLASS (type, i));
  432.         obstack_int_grow (&search_obstack, via_private);
  433.         tail += 2;
  434.         if (tail >= search_stack->limit)
  435.           abort ();
  436.       }
  437.     else if (protect && ! CLASSTYPE_VIA_VIRTUAL (type, i))
  438.       {
  439.         error_with_aggr_type (parent, "type `%s' is ambiguous base class for type `%s'",
  440.                   TYPE_NAME_STRING (xtype));
  441.         error ("(base class for types `%s' and `%s')",
  442.            TYPE_NAME_STRING (type),
  443.            TYPE_NAME_STRING (otype));
  444.         rval = error_mark_node;
  445.         break;
  446.       }
  447.  
  448.     dont_queue:
  449.       /* Process head of queue, if one exists.  */
  450.       if (head >= tail)
  451.     break;
  452.  
  453.       type = search_stack->first[head++];
  454.       is_private = (int)search_stack->first[head++];
  455.       if (TYPE_MAIN_VARIANT (type) == parent)
  456.     {
  457.       if (rval == 0)
  458.         {
  459.           rval = type;
  460.           rval_private = is_private;
  461.         }
  462.       goto dont_queue;
  463.     }
  464.     }
  465.   {
  466.     tree *tp = search_stack->first;
  467.     tree *search_tail = tp + tail;
  468.  
  469.     while (tp < search_tail)
  470.       {
  471.     CLASSTYPE_MARKED5 (*tp) = 0;
  472.     tp += 2;
  473.       }
  474.   }
  475.   search_stack = pop_search_level (search_stack);
  476.  
  477.   if (rval == error_mark_node)
  478.     return error_mark_node;
  479.  
  480.   if (rval && protect && rval_private)
  481.     {
  482.       if (protect == 3)
  483.     {
  484.       int i;
  485.  
  486.       for (i = 1; i <= CLASSTYPE_N_BASECLASSES (xtype); i++)
  487.         if (parent == TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (xtype, i)))
  488.           /* It's ok, since it's immedate.  */
  489.           return rval;
  490.     }
  491.       error ("type `%s' is derived from private `%s'",
  492.          TYPE_NAME_STRING (xtype),
  493.          TYPE_NAME_STRING (parent));
  494.       return error_mark_node;
  495.     }
  496.  
  497.   return rval;
  498. }
  499.  
  500. /* Return the number of levels between type PARENT and type TYPE,
  501.    following the leftmost path to PARENT.  If PARENT
  502.    appears in different places from TYPE's point of view,
  503.    the leftmost PARENT will be the one chosen.
  504.  
  505.    Return -1 if TYPE is not derived from PARENT.
  506.    Return -2 if PARENT is an ambiguous base class of TYPE.
  507.    Return -3 if PARENT is private to TYPE, and protect is non-zero.
  508.  
  509.    If PATH_PTR is non-NULL, then also build the list of types
  510.    from PARENT to TYPE, with TREE_VIA_VIRUAL and TREE_VIA_PUBLIC
  511.    set.  */
  512. get_base_distance (parent, type, protect, path_ptr)
  513.      register tree parent, type;
  514.      int protect;
  515.      tree *path_ptr;
  516. {
  517.   int head = 0, tail = 0;
  518.   int is_private = 0;
  519.   int rval;
  520.   int depth = 0;
  521.   int rval_private = 0;
  522.   tree basetypes;
  523.   tree friends = current_class_type
  524.     ? CLASSTYPE_FRIEND_CLASSES (type) : NULL_TREE;
  525.  
  526.   parent = TYPE_MAIN_VARIANT (parent);
  527.  
  528.   if (path_ptr)
  529.     basetypes = CLASSTYPE_AS_LIST (type);
  530.  
  531.   if (parent == type)
  532.     {
  533.       /* If the distance is 0, then we don't really need
  534.      a path pointer, but we shouldn't let garbage go back.  */
  535.       if (path_ptr)
  536.     *path_ptr = basetypes;
  537.       return 0;
  538.     }
  539.  
  540.   search_stack = push_search_level (search_stack, &search_obstack);
  541.  
  542.   while (1)
  543.     {
  544.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  545.  
  546.       /* Process and/or queue base types.  */
  547.       for (i = 1; i <= n_baselinks; i++)
  548.     if (CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) == 0)
  549.       {
  550.         tree btypes;
  551.  
  552.         int via_private = is_private || !CLASSTYPE_VIA_PUBLIC (type, i);
  553.  
  554.         if (via_private == 0)
  555.           ;
  556.         else if (protect == 0)
  557.           via_private = 0;
  558. #if 0
  559.         /* 13 Jan, 1990: I guess this is turned off because
  560.            `get_base_type' will emit a more eloquent message
  561.            if a message desired [--Michael].  */
  562.         /* The immediate base class of the class we are in
  563.            does let its public members through.  */
  564.         else if (type == current_class_type)
  565.           via_private = 0;
  566.         else if (protect
  567.              && friends != NULL_TREE
  568.              && type == xtype
  569.              && value_member (current_class_type, friends))
  570.           /* Friend types of the most derived type have access
  571.          to its baseclass pointers.  */
  572.           via_private = 0;
  573. #endif
  574.  
  575.         CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) = 1;
  576.         obstack_ptr_grow (&search_obstack, CLASSTYPE_BASECLASS (type, i));
  577.  
  578.         obstack_ptr_grow (&search_obstack, depth);
  579.         obstack_int_grow (&search_obstack, via_private);
  580.         if (path_ptr)
  581.           {
  582.         btypes = tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  583.                     basetypes);
  584.         TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  585.         TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  586.         obstack_ptr_grow (&search_obstack, btypes);
  587.         tail += 1;
  588.           }
  589.         tail += 3;
  590.         if (tail >= search_stack->limit)
  591.           abort ();
  592.       }
  593.     else if (! CLASSTYPE_VIA_VIRTUAL (type, i))
  594.       {
  595.         rval = -2;
  596.         goto done;
  597.       }
  598.  
  599.       /* Process head of queue, if one exists.  */
  600.       if (head >= tail)
  601.     {
  602.       rval = -1;
  603.       break;
  604.     }
  605.  
  606.       type = search_stack->first[head++];
  607.       depth = (int)search_stack->first[head++] + 1;
  608.       is_private = (int)search_stack->first[head++];
  609.       if (path_ptr)
  610.     basetypes = search_stack->first[head++];
  611.       if (TYPE_MAIN_VARIANT (type) == parent)
  612.     {
  613.       rval = depth;
  614.       rval_private = is_private;
  615.       break;
  616.     }
  617.     }
  618.  done:
  619.   {
  620.     tree *tp = search_stack->first;
  621.     tree *search_tail = tp + tail;
  622.     int increment = path_ptr ? 4 : 3;
  623.  
  624.     while (tp < search_tail)
  625.       {
  626.     CLASSTYPE_MARKED5 (*tp) = 0;
  627.     tp += increment;
  628.       }
  629.  
  630.     /* Now, guarantee that we are following the leftmost
  631.        path in the chain.  */
  632.     if (rval > 0
  633.     && (DECL_OFFSET (TYPE_NAME (type)) != 0
  634.         || TREE_VIA_VIRTUAL (type)))
  635.       {
  636.     /* Reduce all types yet to be fully processed into
  637.        the base type we are looking for, or NULL_TREE.  */
  638.     for (tp = search_stack->first; tp < search_tail; tp += increment)
  639.       {
  640.         tree *sub_tp, sub_path_ptr;
  641.         int sub_rval;
  642.  
  643.         /* Don't chase down more right-most paths.  */
  644.         if (TREE_VIA_VIRTUAL (*tp)
  645.         || DECL_OFFSET (TYPE_NAME (*tp)) > DECL_OFFSET (TYPE_NAME (type)))
  646.           {
  647.         *tp = NULL_TREE;
  648.         continue;
  649.           }
  650.  
  651.         /* Don't hassle with duplicates.  */
  652.         if (*tp == type)
  653.           goto skip;
  654.  
  655.         for (sub_tp = search_stack->first; sub_tp < tp; sub_tp += increment)
  656.           if (*tp == *sub_tp)
  657.         goto skip;
  658.  
  659.         /* Find this type's TYPE basetype, if it has one.  */
  660.         sub_rval = get_base_distance (parent, *tp, 0, &sub_path_ptr);
  661.         if (sub_rval == -1)
  662.           *tp = NULL_TREE;
  663.         else
  664.           {
  665.         if (path_ptr && TREE_CHAIN (tp[3]))
  666.           {
  667.             tree last;
  668.             tree next_to_last = sub_path_ptr;
  669.             while (TREE_CHAIN (next_to_last)
  670.                && TREE_CHAIN (TREE_CHAIN (next_to_last)))
  671.               next_to_last = TREE_CHAIN (next_to_last);
  672.             if (next_to_last == sub_path_ptr)
  673.               {
  674.             sub_path_ptr = copy_node (sub_path_ptr);
  675.             last = sub_path_ptr;
  676.               }
  677.             else
  678.               {
  679.             last = copy_node (TREE_CHAIN (next_to_last));
  680.             TREE_CHAIN (next_to_last) = last;
  681.               }
  682.             TREE_CHAIN (last) = TREE_CHAIN (tp[3]);
  683.           }
  684.         *tp = TREE_VALUE (sub_path_ptr);
  685.         if (path_ptr)
  686.           tp[3] = sub_path_ptr;
  687.           }
  688.       skip: {}
  689.       }
  690.  
  691.     /* For all the types which reduce to TYPE, choose
  692.        the leftmost non-virtual one of them.  */
  693.     for (tp = search_stack->first; tp < search_tail; tp += increment)
  694.       {
  695.         if (*tp == NULL_TREE)
  696.           continue;
  697.  
  698.         if (DECL_OFFSET (TYPE_NAME (*tp)) < DECL_OFFSET (TYPE_NAME (type)))
  699.           {
  700.         rval = -2;
  701.         type = *tp;
  702.         if (path_ptr)
  703.           basetypes = tp[3];
  704.           }
  705.       }
  706.     if (rval == -2)
  707.       rval_private = 0;
  708.       }
  709.   }
  710.   search_stack = pop_search_level (search_stack);
  711.  
  712.   if (rval && protect && rval_private)
  713.     return -3;
  714.  
  715.   if (path_ptr)
  716.     *path_ptr = basetypes;
  717.   return rval;
  718. }
  719.  
  720. /* Search for a member with name NAME in a multiple inheritance lattice
  721.    specified by TYPE.  If it does not exist, return NULL_TREE.
  722.    If the member is ambiguously referenced, return `error_mark_node'.
  723.    Otherwise, return the FIELD_DECL.  */
  724.  
  725. /* Do a 1-level search for NAME as a member of TYPE.  The caller
  726.    must figure out whether it has a visible path to this field.
  727.    (Since it is only one level, this is reasonable.)  */
  728. static tree
  729. lookup_field_1 (type, name)
  730.      tree type, name;
  731. {
  732.   register tree field = TYPE_FIELDS (type);
  733.  
  734. #ifdef GATHER_STATISTICS
  735.   n_calls_lookup_field_1++;
  736. #endif
  737.   while (field)
  738.     {
  739. #ifdef GATHER_STATISTICS
  740.       n_fields_searched++;
  741. #endif
  742.       if (DECL_ANON_UNION_ELEM (field))
  743.     {
  744.       tree temp = lookup_field_1 (TREE_TYPE (field), name);
  745.       if (temp)
  746.         return temp;
  747.     }
  748.       if (DECL_NAME (field) == name)
  749.     return field;
  750.       field = TREE_CHAIN (field);
  751.     }
  752.   /* Not found.  */
  753.   if (name == _vptr_name)
  754.     {
  755.       /* Give the user what s/he thinks s/he wants.  */
  756.       if (TYPE_VIRTUAL_P (type))
  757.     return CLASSTYPE_VFIELD (type);
  758.     }
  759.   return NULL_TREE;
  760. }
  761.  
  762. /* Compute the visibility of FIELD.  This is done by computing
  763.    the visibility available to each type in BASETYPES (which comes
  764.    as a list of [via_public/basetype] in reverse order, namely base
  765.    class before derived class).  The first one which defines a
  766.    visibility defines the visibility for the field.  Otherwise, the
  767.    visibility of the field is that which occurs normally.
  768.  
  769.    Uses global variables CURRENT_CLASS_TYPE and
  770.    CURRENT_FUNCTION_DECL to use friend relationships
  771.    if necessary.
  772.  
  773.    This will be static when lookup_fnfield comes into this file.  */
  774.  
  775. #define PUBLIC_RETURN do { TREE_FIELD_PUBLIC (field) = 1; return visibility_public; } while (0)
  776. #define PROTECTED_RETURN do { TREE_FIELD_PROTECTED (field) = 1; return visibility_protected; } while (0)
  777. #define PRIVATE_RETURN do { TREE_FIELD_PRIVATE (field) = 1; return visibility_private; } while (0)
  778.  
  779. enum visibility_type
  780. compute_visibility (basetypes, field)
  781.      tree basetypes, field;
  782. {
  783.   enum visibility_type visibility = visibility_public;
  784.   tree types, type;
  785.   tree context = DECL_FIELD_CONTEXT (field);
  786.  
  787.   /* Virtual function tables are never private.
  788.      But we should know that we are looking for this,
  789.      and not even try to hide it.  */
  790.   if (VFIELD_NAME_P (DECL_NAME (field)) == 1)
  791.     return visibility_public;
  792.  
  793.   /* Make these special cases fast.  */
  794.   if (TREE_VALUE (basetypes) == current_class_type)
  795.     {
  796.       if (TREE_FIELD_PUBLIC (field))
  797.     return visibility_public;
  798.       if (TREE_FIELD_PROTECTED (field))
  799.     return visibility_protected;
  800.       if (TREE_FIELD_PRIVATE (field))
  801.     return visibility_private;
  802.     }
  803.  
  804.   /* Member function manipulating its own members.  */
  805.   if (current_class_type == context)
  806.     PUBLIC_RETURN;
  807.  
  808.   /* Member found immediately within object.  */
  809.   if (TREE_CHAIN (basetypes) == NULL_TREE)
  810.     {
  811.       /* At object's top level, public members are public.  */
  812.       if (TREE_PROTECTED (field) == 0 && TREE_PRIVATE (field) == 0)
  813.     PUBLIC_RETURN;
  814.  
  815.       /* Friend function manipulating members it gets (for being a friend).  */
  816.       if (is_friend (context, current_function_decl))
  817.     PUBLIC_RETURN;
  818.  
  819.       /* Inner than that, without special visibility,
  820.  
  821.        protected members are ok if type of object is current_class_type
  822.        is derived therefrom.  This means that if the type of the object
  823.        is a base type for our current class type, we cannot access
  824.        protected members.
  825.  
  826.        private members are not ok.  */
  827.       if (current_class_type && DECL_VISIBILITY (field) == NULL_TREE)
  828.     {
  829.       if (TREE_PRIVATE (field))
  830.         PRIVATE_RETURN;
  831.  
  832.       if (TREE_PROTECTED (field))
  833.         {
  834.           if (context == current_class_type
  835.           || (type = get_base_type (current_class_type, context, 0)))
  836.         PUBLIC_RETURN;
  837.           else
  838.         PROTECTED_RETURN;
  839.         }
  840.       else abort ();
  841.     }
  842.     }
  843.   /* Friend function manipulating members it gets (for being a friend).  */
  844.   if (is_friend (context, current_function_decl))
  845.     PUBLIC_RETURN;
  846.  
  847.   /* must reverse more than one element */
  848.   basetypes = nreverse (basetypes);
  849.  
  850.   types = basetypes;
  851.  
  852.   while (types)
  853.     {
  854.       tree member;
  855.       type = TYPE_MAIN_VARIANT (TREE_VALUE (types));
  856.  
  857.       member = purpose_member (type, DECL_VISIBILITY (field));
  858.       if (member)
  859.     {
  860.       visibility = (enum visibility_type)TREE_VALUE (member);
  861.       if (visibility == visibility_public
  862.           || is_friend (type, current_function_decl)
  863.           || (visibility == visibility_protected
  864.           && current_class_type
  865.           && get_base_type (context, current_class_type, 0)))
  866.         visibility = visibility_public;
  867.       goto ret;
  868.     }
  869.  
  870.       /* Friends inherit the visibility of the class they inherit from.  */
  871.       if (is_friend (type, current_function_decl))
  872.     {
  873.       if (type == context)
  874.         {
  875.           visibility = visibility_public;
  876.           goto ret;
  877.         }
  878.       if (TREE_PROTECTED (field))
  879.         {
  880.           visibility = visibility_public;
  881.           goto ret;
  882.         }
  883.       if (visibility == visibility_public)
  884.         goto ret;
  885.       /* else, may be a friend of a deeper base class */
  886.     }
  887.  
  888.       if (type == context)
  889.     break;
  890.  
  891.       types = TREE_CHAIN (types);
  892.       /* If the next type was not VIA_PUBLIC, then fields of all
  893.      remaining class past that one are private.  */
  894.       if (types && ! TREE_VIA_PUBLIC (types))
  895.     visibility = visibility_private;
  896.     }
  897.  
  898.   /* No special visibilities apply.  Use normal rules.
  899.      No assignment needed for BASETYPEs here from the nreverse.
  900.      This is because we use it only for information about the
  901.      path to the base.  The code earlier dealt with what
  902.      happens when we are at the base level.  */
  903.  
  904.   if (visibility == visibility_public)
  905.     {
  906.       basetypes = nreverse (basetypes);
  907.       if (TREE_PRIVATE (field))
  908.     PRIVATE_RETURN;
  909.       if (TREE_PROTECTED (field))
  910.     {
  911.       /* Used to check if the current class type was derived from
  912.          the type that contains the field.  This is wrong for
  913.          multiple inheritance because is gives one class reference
  914.          to protected members via another classes protected path.
  915.          I.e., if A; B1 : A; B2 : A;  Then B1 and B2 can access
  916.          their own members which are protected in A, but not
  917.          those same members in one another.  */
  918.       if (
  919. #if 1
  920.           current_class_type
  921.           && get_base_type (context, current_class_type, 0)
  922. #else
  923.           current_class_type
  924.           && value_member (current_class_type, basetypes)
  925. #endif
  926.           )
  927.         PUBLIC_RETURN;
  928.       PROTECTED_RETURN;
  929.     }
  930.       PUBLIC_RETURN;
  931.     }
  932.  
  933.   if (visibility == visibility_private
  934.       && current_class_type != NULL_TREE)
  935.     {
  936.       if (TREE_PRIVATE (field))
  937.     {
  938.       nreverse (basetypes);
  939.       PRIVATE_RETURN;
  940.     }
  941.  
  942.       /* See if the field isn't protected.  */
  943.       if (TREE_PROTECTED (field))
  944.     {
  945.       tree test;
  946. #if 0
  947.       test = get_base_type (type, current_class_type, 0);
  948. #else
  949.       test = value_member (current_class_type, basetypes);
  950. #endif
  951.       nreverse (basetypes);
  952.       if (test)
  953.         PUBLIC_RETURN;
  954.       PROTECTED_RETURN;
  955.     }
  956.  
  957.       /* See if the field isn't a public member of
  958.      a private base class.  */
  959.  
  960.       visibility = visibility_public;
  961.       types = TREE_CHAIN (basetypes);
  962.       while (types)
  963.     {
  964.       if (! TREE_VIA_PUBLIC (types))
  965.         {
  966.           if (visibility == visibility_private)
  967.         {
  968.           visibility = visibility_private;
  969.           goto ret;
  970.         }
  971.           visibility = visibility_private;
  972.         }
  973.       if (TYPE_MAIN_VARIANT (TREE_VALUE (types)) == context)
  974.         {
  975.           visibility = visibility_public;
  976.           goto ret;
  977.         }
  978.       types = TREE_CHAIN (types);
  979.     }
  980.       abort ();
  981.     }
  982.  
  983.  ret:
  984.   nreverse (basetypes);
  985.  
  986.   if (visibility == visibility_public)
  987.     TREE_FIELD_PUBLIC (field) = 1;
  988.   else if (visibility == visibility_protected)
  989.     TREE_FIELD_PROTECTED (field) = 1;
  990.   else if (visibility == visibility_private)
  991.     TREE_FIELD_PRIVATE (field) = 1;
  992.   else abort ();
  993.   return visibility;
  994. }
  995.  
  996. /* Make an entry in the memoized table for type TYPE
  997.    that the entry for NAME is FIELD.  */
  998.  
  999. tree
  1000. make_memoized_table_entry (type, name, function_p)
  1001.      tree type, name;
  1002.      int function_p;     
  1003. {
  1004.   int index = MEMOIZED_HASH_FN (name);
  1005.   tree entry, *prev_entry;
  1006.  
  1007.   memoized_adds[function_p] += 1;
  1008.   if (CLASSTYPE_MTABLE_ENTRY (type) == NULL_TREE)
  1009.     {
  1010.       obstack_ptr_grow (&type_obstack, type);
  1011.       obstack_blank (&type_obstack, sizeof (struct memoized_entry *));
  1012.       CLASSTYPE_MTABLE_ENTRY (type) = my_new_memoized_entry (0);
  1013.       type_stack->len++;
  1014.       if (type_stack->len * 2 >= type_stack->base.limit)
  1015.     abort ();
  1016.     }
  1017.   if (function_p)
  1018.     prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1019.   else
  1020.     prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1021.  
  1022.   entry = my_tree_cons (name, 0, *prev_entry);
  1023.   *prev_entry = entry;
  1024.  
  1025.   /* Don't know the error message to give yet.  */
  1026.   TREE_TYPE (entry) = error_mark_node;
  1027.  
  1028.   return entry;
  1029. }
  1030.  
  1031. tree
  1032. lookup_field (xbasetype, name, protect)
  1033.      register tree xbasetype, name;
  1034.      int protect;
  1035. {
  1036.   int head = 0, tail = 0;
  1037.   tree type, rval;
  1038.   tree basetype, basetypes;
  1039.   enum visibility_type this_v = visibility_default;
  1040.   tree entry;
  1041.   enum visibility_type own_visibility = visibility_default;
  1042.   int vbase_name_p = VBASE_NAME_P (name);
  1043.  
  1044.   /* Things for memoization.  */
  1045.   char *errstr = 0;
  1046.  
  1047.   /* Set this to nonzero if we don't know how to compute
  1048.      accurate error messages for visibility.  */
  1049.   int index = MEMOIZED_HASH_FN (name);
  1050.  
  1051.   if (TREE_CODE (xbasetype) == TREE_LIST)
  1052.     basetypes = xbasetype, basetype = TREE_VALUE (xbasetype);
  1053.   else
  1054.     basetypes = CLASSTYPE_AS_LIST (xbasetype), basetype = xbasetype;
  1055.  
  1056.   if (CLASSTYPE_MTABLE_ENTRY (basetype))
  1057.     {
  1058.       tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (basetype), index);
  1059.  
  1060.       while (tem && TREE_PURPOSE (tem) != name)
  1061.     {
  1062.       memoized_fields_searched[0]++;
  1063.       tem = TREE_CHAIN (tem);
  1064.     }
  1065.       if (tem)
  1066.     {
  1067.       if (protect && TREE_TYPE (tem))
  1068.         {
  1069.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1070.              IDENTIFIER_POINTER (name),
  1071.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
  1072.           return error_mark_node;
  1073.         }
  1074.       if (TREE_VALUE (tem) == NULL_TREE)
  1075.         memoized_fast_rejects[0] += 1;
  1076.       else
  1077.         memoized_fast_finds[0] += 1;
  1078.       return TREE_VALUE (tem);
  1079.     }
  1080.     }
  1081.  
  1082. #ifdef GATHER_STATISTICS
  1083.   n_calls_lookup_field++;
  1084. #endif
  1085.   if (flag_memoize_lookups && ! global_bindings_p ())
  1086.     entry = make_memoized_table_entry (basetype, name, 0);
  1087.   else
  1088.     entry = 0;
  1089.  
  1090.   rval = lookup_field_1 (basetype, name);
  1091.  
  1092.   if (rval)
  1093.     {
  1094.       if (flag_memoize_lookups || protect)
  1095.     {
  1096.       if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
  1097.         this_v = compute_visibility (basetypes, rval);
  1098.       if (TREE_CODE (rval) == CONST_DECL)
  1099.         {
  1100.           if (this_v == visibility_private)
  1101.         errstr = "enum `%s' is a private value of class `%s'";
  1102.           else if (this_v == visibility_protected)
  1103.         errstr = "enum `%s' is a protected value of class `%s'";
  1104.         }
  1105.       else
  1106.         {
  1107.           if (this_v == visibility_private)
  1108.         errstr = "member `%s' is a private member of class `%s'";
  1109.           else if (this_v == visibility_protected)
  1110.         errstr = "member `%s' is a protected member of class `%s'";
  1111.         }
  1112.     }
  1113.  
  1114.       if (entry)
  1115.     {
  1116.       if (errstr)
  1117.         {
  1118.           /* This depends on behavior of lookup_field_1!  */
  1119.           tree error_string = my_build_string (errstr);
  1120.           TREE_TYPE (entry) = error_string;
  1121.         }
  1122.       else
  1123.         {
  1124.           /* Let entry know there is no problem with this access.  */
  1125.           TREE_TYPE (entry) = NULL_TREE;
  1126. #if 0
  1127.           /* And since everything is ok, bear the
  1128.          cost of generating correct code.  */
  1129.           if (DECL_OFFSET (TYPE_NAME (basetype)) != 0
  1130.           || TREE_VIA_VIRTUAL (basetype))
  1131.         {
  1132.           rval = my_copy_node (rval);
  1133.           DECL_FIELD_CONTEXT (rval) = basetype;
  1134.         }
  1135. #endif
  1136.         }
  1137.       TREE_VALUE (entry) = rval;
  1138.     }
  1139. #if 0
  1140.       else if ((DECL_OFFSET (TYPE_NAME (basetype)) != 0
  1141.         || TREE_VIA_VIRTUAL (basetype))
  1142.            && ! (errstr && protect))
  1143.     {
  1144.       rval = my_copy_node (rval);
  1145.       DECL_FIELD_CONTEXT (rval) = basetype;
  1146.     }
  1147. #endif
  1148.  
  1149.       if (errstr && protect)
  1150.     {
  1151.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (basetype));
  1152.       return error_mark_node;
  1153.     }
  1154.       return rval;
  1155.     }
  1156.  
  1157.   type = TYPE_MAIN_VARIANT (basetype);
  1158.  
  1159.   search_stack = push_search_level (search_stack, &search_obstack);
  1160.   TREE_VIA_PUBLIC (basetypes) = 1;
  1161.  
  1162.   while (1)
  1163.     {
  1164.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1165.  
  1166.       /* Process and/or queue base types.  */
  1167.       for (i = 1; i <= n_baselinks; i++)
  1168.     {
  1169.       if (CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0)
  1170.         {
  1171.           tree btypes;
  1172.  
  1173.           CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) = 1;
  1174.           btypes = my_tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1175.                      basetypes);
  1176.           TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  1177.           TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  1178.           obstack_ptr_grow (&search_obstack, btypes);
  1179.           tail += 1;
  1180.           if (tail >= search_stack->limit)
  1181.         abort ();
  1182.         }
  1183.     }
  1184.  
  1185.       /* Process head of queue, if one exists.  */
  1186.       if (head >= tail)
  1187.     break;
  1188.  
  1189.       basetypes = search_stack->first[head++];
  1190.       type = TREE_VALUE (basetypes);
  1191.  
  1192.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1193.      and we do find NAME in TYPE, verify that such a second
  1194.      sighting is in fact legal.  */
  1195.  
  1196.       if (rval)
  1197.     {
  1198.       /* Just another way of finding the same member.  */
  1199.       if (DECL_FIELD_CONTEXT (rval) == type)
  1200.         {
  1201.           enum visibility_type new_v
  1202.         = compute_visibility (basetypes, rval);
  1203.           if (this_v != new_v)
  1204.         errstr = "conflicting visibilities to member `%s'";
  1205.         }
  1206.       /* Same baseclass, different places in the lattice.  */
  1207.       else if (DECL_FIELD_CONTEXT (rval) == TYPE_MAIN_VARIANT (type))
  1208.         errstr = "member `%s' belongs to distinct base classes `%s'";
  1209.       else
  1210.         {
  1211.           tree nval = lookup_field_1 (type, name);
  1212.  
  1213.           if (nval && get_base_type (type, DECL_FIELD_CONTEXT (rval), 0) == 0)
  1214.         {
  1215.           /* We found it in other than a baseclass of RVAL's.  */
  1216.           errstr = "request for member `%s' is ambiguous";
  1217.         }
  1218.         }
  1219.       if (errstr && entry)
  1220.         {
  1221.           tree error_string = my_build_string (errstr);
  1222.           TREE_TYPE (entry) = error_string;
  1223.         }
  1224.       if (errstr && protect)
  1225.         break;
  1226.     }
  1227.       else
  1228.     {
  1229.       rval = lookup_field_1 (type, name);
  1230.       if (rval)
  1231.         {
  1232. #if 0
  1233.           if (DECL_OFFSET (TYPE_NAME (type)) != 0
  1234.           || TREE_VIA_VIRTUAL (type))
  1235.         {
  1236.           rval = my_copy_node (rval);
  1237.           DECL_FIELD_CONTEXT (rval) = type;
  1238.         }
  1239. #endif
  1240.  
  1241.           if (entry || protect)
  1242.         this_v = compute_visibility (basetypes, rval);
  1243.           if (entry)
  1244.         TREE_VALUE (entry) = rval;
  1245.  
  1246.           /* These may look ambiguous, but they really are not.  */
  1247.           if (vbase_name_p)
  1248.         break;
  1249.         }
  1250.     }
  1251.     }
  1252.   {
  1253.     tree *tp = search_stack->first;
  1254.     tree *search_tail = tp + tail;
  1255.  
  1256.     /* If this FIELD_DECL defines its own visibility, deal with that.  */
  1257.     if (rval && errstr == 0
  1258.     && DECL_VISIBILITY (rval)
  1259.     && (protect || entry))
  1260.       {
  1261.     while (tp < search_tail)
  1262.       {
  1263.         /* If is possible for one of the derived types on the
  1264.            path to have defined special visibility for this
  1265.            field.  Look for such declarations and report an
  1266.            error if a conflict is found.  */
  1267.         enum visibility_type new_v;
  1268.  
  1269.         if (this_v != visibility_default)
  1270.           new_v = compute_visibility (*tp, rval);
  1271.         if (this_v != visibility_default && new_v != this_v)
  1272.           {
  1273.         errstr = "conflicting visibilities to member `%s'";
  1274.         this_v = visibility_default;
  1275.           }
  1276.         own_visibility = new_v;
  1277.         CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1278.       }
  1279.       }
  1280.     else
  1281.       {
  1282.     while (tp < search_tail)
  1283.       CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1284.       }
  1285.   }
  1286.   search_stack = pop_search_level (search_stack);
  1287.  
  1288.   if (errstr == 0)
  1289.     {
  1290.       if (own_visibility == visibility_private)
  1291.     errstr = "member `%s' declared private";
  1292.       else if (own_visibility == visibility_protected)
  1293.     errstr = "member `%s' declared protected";
  1294.       else if (this_v == visibility_private)
  1295.     errstr = TREE_PRIVATE (rval) ? "member `%s' is private" : "member `%s' is from private base class";
  1296.       else if (this_v == visibility_protected)
  1297.     errstr = "member `%s' is protected";
  1298.     }
  1299.  
  1300.   if (entry)
  1301.     {
  1302.       if (errstr)
  1303.     {
  1304.       tree error_string = my_build_string (errstr);
  1305.       /* Save error message with entry.  */
  1306.       TREE_TYPE (entry) = error_string;
  1307.     }
  1308.       else
  1309.     {
  1310.       /* Mark entry as having no error string.  */
  1311.       TREE_TYPE (entry) = NULL_TREE;
  1312.     }
  1313.     }
  1314.  
  1315.   if (errstr && protect)
  1316.     {
  1317.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (type));
  1318.       rval = error_mark_node;
  1319.     }
  1320.   return rval;
  1321. }
  1322.  
  1323. /* TYPE is a class type. Return the index of the fields within
  1324.    the method vector with name NAME, or -1 is no such field exists.  */
  1325. static int
  1326. lookup_fnfields_1 (type, name)
  1327.      tree type, name;
  1328. {
  1329.   register tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1330.  
  1331.   if (method_vec != 0)
  1332.     {
  1333.       register tree *methods = &TREE_VEC_ELT (method_vec, 0);
  1334.       register tree *end = TREE_VEC_END (method_vec);
  1335.  
  1336. #ifdef GATHER_STATISTICS
  1337.       n_calls_lookup_fnfields_1++;
  1338. #endif
  1339.       if (*methods == 0)
  1340.     methods++;
  1341.       while (methods != end)
  1342.     {
  1343. #ifdef GATHER_STATISTICS
  1344.       n_outer_fields_searched++;    
  1345. #endif
  1346.       if (DECL_ORIGINAL_NAME (*methods) == name)
  1347.         break;
  1348.       methods++;
  1349.     }
  1350.       if (methods != end)
  1351.     return methods - &TREE_VEC_ELT (method_vec, 0);
  1352.     }
  1353.  
  1354.   return -1;
  1355. }
  1356.  
  1357. /* Given a list of member functions FIELDS (which are implicitly
  1358.    named TREE_PURPOSE (FIELDS), and come from base type
  1359.    DECL_FIELD_CONTEXT (TREE_VALUE (FIELDS))), attempt to find the
  1360.    actual method which can accept (using conversions) PARMS.
  1361.    The types of PARMS are already computed in PARMTYPES.  */
  1362. tree
  1363. lookup_fnfield (fields, parms, parmtypes)
  1364.      tree fields, parms, parmtypes;
  1365. {
  1366.   abort ();
  1367. }
  1368.  
  1369. /* Starting from BASETYPE, return a TREE_BASELINK-like object
  1370.    which gives the following information (in a list):
  1371.  
  1372.    TREE_TYPE: list of basetypes needed to get to...
  1373.    TREE_VALUE: list of all functions in of given type
  1374.    which have name NAME.
  1375.  
  1376.    No visibility information is computed by this function,
  1377.    other then to adorn the list of basetypes with
  1378.    TREE_VIA_PUBLIC.
  1379.  
  1380.    If FIND_AMBIGUOUS is non-zero, then if we find two ways to get
  1381.    to the same member function, both those ways are found,
  1382.    and the caller must know what to do about this.  */
  1383. tree
  1384. lookup_fnfields (basetypes, name, find_ambiguous)
  1385.      tree basetypes, name;
  1386.      int find_ambiguous;
  1387. {
  1388.   int head = 0, tail = 0;
  1389.   tree type, rval, rvals = NULL_TREE;
  1390.   tree basetype;
  1391.   tree entry;
  1392.  
  1393.   /* For now, don't try this.  */
  1394.   int protect = find_ambiguous;
  1395.  
  1396.   /* Things for memoization.  */
  1397.   char *errstr = 0;
  1398.  
  1399.   /* Set this to nonzero if we don't know how to compute
  1400.      accurate error messages for visibility.  */
  1401.   int index = MEMOIZED_HASH_FN (name);
  1402.  
  1403.   basetype = TREE_VALUE (basetypes);
  1404.  
  1405.   if (CLASSTYPE_MTABLE_ENTRY (basetype))
  1406.     {
  1407.       tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (basetype), index);
  1408.  
  1409.       while (tem && TREE_PURPOSE (tem) != name)
  1410.     {
  1411.       memoized_fields_searched[1]++;
  1412.       tem = TREE_CHAIN (tem);
  1413.     }
  1414.       if (tem)
  1415.     {
  1416.       if (protect && TREE_TYPE (tem))
  1417.         {
  1418.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1419.              IDENTIFIER_POINTER (name),
  1420.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
  1421.           return error_mark_node;
  1422.         }
  1423.       if (TREE_VALUE (tem) == NULL_TREE)
  1424.         {
  1425.           memoized_fast_rejects[1] += 1;
  1426.           return NULL_TREE;
  1427.         }
  1428.       else
  1429.         {
  1430.           /* Want to return this, but we must make sure
  1431.          that visibility information is consistent.  */
  1432.           tree baselink = TREE_VALUE (tem);
  1433.           tree memoized_basetypes = TREE_PURPOSE (baselink);
  1434.           tree these_basetypes = basetypes;
  1435.           while (memoized_basetypes && these_basetypes)
  1436.         {
  1437.           memoized_fields_searched[1]++;
  1438.           if (TREE_VALUE (memoized_basetypes) != TREE_VALUE (these_basetypes))
  1439.             break;
  1440.           memoized_basetypes = TREE_CHAIN (memoized_basetypes);
  1441.           these_basetypes = TREE_CHAIN (these_basetypes);
  1442.         }
  1443.           if (memoized_basetypes == these_basetypes)
  1444.         {
  1445.           memoized_fast_finds[1] += 1;
  1446.           return TREE_VALUE (tem);
  1447.         }
  1448.           /* else, we must re-find this field by hand.  */
  1449.           baselink = tree_cons (basetypes, TREE_VALUE (baselink), TREE_CHAIN (baselink));
  1450.           return baselink;
  1451.         }
  1452.     }
  1453.     }
  1454.  
  1455. #ifdef GATHER_STATISTICS
  1456.   n_calls_lookup_fnfields++;
  1457. #endif
  1458.   if (flag_memoize_lookups && ! global_bindings_p ())
  1459.     entry = make_memoized_table_entry (basetype, name, 1);
  1460.   else
  1461.     entry = 0;
  1462.  
  1463.   index = lookup_fnfields_1 (basetype, name);
  1464.  
  1465.   if (index >= 0)
  1466.     {
  1467.       rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), index);
  1468.       rvals = my_tree_cons (basetypes, rval, NULL_TREE);
  1469.       if (CLASSTYPE_BASELINK_VEC (basetype))
  1470.     TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (basetype), index);
  1471.  
  1472.       if (entry)
  1473.     {
  1474.       TREE_VALUE (entry) = rvals;
  1475.       TREE_TYPE (entry) = NULL_TREE;
  1476.     }
  1477.  
  1478.       if (errstr && protect)
  1479.     {
  1480.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (basetype));
  1481.       return error_mark_node;
  1482.     }
  1483.       return rvals;
  1484.     }
  1485.   rval = NULL_TREE;
  1486.   type = TYPE_MAIN_VARIANT (basetype);
  1487.  
  1488.   search_stack = push_search_level (search_stack, &search_obstack);
  1489.   TREE_VIA_PUBLIC (basetypes) = 1;
  1490.  
  1491.   while (1)
  1492.     {
  1493.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1494.  
  1495.       /* Process and/or queue base types.  */
  1496.       for (i = 1; i <= n_baselinks; i++)
  1497.     {
  1498.       if (CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0)
  1499.         {
  1500.           tree btypes;
  1501.  
  1502.           CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) = 1;
  1503.           btypes = my_tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1504.                      basetypes);
  1505.           TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  1506.           TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  1507.           obstack_ptr_grow (&search_obstack, btypes);
  1508.           tail += 1;
  1509.           if (tail >= search_stack->limit)
  1510.         abort ();
  1511.         }
  1512.     }
  1513.  
  1514.       /* Process head of queue, if one exists.  */
  1515.       if (head >= tail)
  1516.     break;
  1517.  
  1518.       basetypes = search_stack->first[head++];
  1519.       type = TREE_VALUE (basetypes);
  1520.  
  1521.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1522.      and we do find NAME in TYPE, verify that such a second
  1523.      sighting is in fact legal.  */
  1524.  
  1525.       if (rval)
  1526.     {
  1527.       tree context = DECL_FIELD_CONTEXT (rval);
  1528.       /* Just another way of finding the same member.  */
  1529.       if (context == type)
  1530.         ;
  1531.       /* Same baseclass, maybe different places in the lattice.  */
  1532.       else if (context == TYPE_MAIN_VARIANT (type))
  1533.         {
  1534.           if (TREE_VIA_VIRTUAL (TREE_PURPOSE (rvals)))
  1535.         if (TREE_VIA_VIRTUAL (basetypes))
  1536.           ;
  1537.         else
  1538.           errstr = "member `%s' belongs to virtual and non-virtual baseclasses `%s'";
  1539.           else if (TREE_VIA_VIRTUAL (basetypes))
  1540.         errstr = "member `%s' belongs to virtual and non-virtual baseclasses `%s'";
  1541.           else
  1542.         errstr = "member `%s' belongs to MI-distinct base classes `%s'";
  1543.         }
  1544.       else
  1545.         {
  1546.           int index = lookup_fnfields_1 (type, name);
  1547.  
  1548.           if (index >= 0 && get_base_type (type, context, 0) == 0)
  1549.         {
  1550.           /* We found it in other than a baseclass of RVAL's.  */
  1551.           rvals = my_tree_cons (basetypes, TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index), rvals);
  1552.           if (CLASSTYPE_BASELINK_VEC (type))
  1553.             TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1554.         }
  1555.         }
  1556.       if (errstr && entry)
  1557.         {
  1558.           tree error_string = my_build_string (errstr);
  1559.           TREE_TYPE (entry) = error_string;
  1560.         }
  1561.       if (errstr && find_ambiguous)
  1562.         {
  1563.           rvals = error_mark_node;
  1564.           break;
  1565.         }
  1566.     }
  1567.       else
  1568.     {
  1569.       int index = lookup_fnfields_1 (type, name);
  1570.       if (index >= 0)
  1571.         {
  1572.           rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1573.           rvals = my_tree_cons (basetypes, rval, NULL_TREE);
  1574.           if (CLASSTYPE_BASELINK_VEC (type))
  1575.         TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1576.           if (entry)
  1577.         TREE_VALUE (entry) = rvals;
  1578.         }
  1579.       else
  1580.         rval = NULL_TREE;
  1581.     }
  1582.     }
  1583.   {
  1584.     tree *tp = search_stack->first;
  1585.     tree *search_tail = tp + tail;
  1586.  
  1587.     while (tp < search_tail)
  1588.       CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1589.   }
  1590.   search_stack = pop_search_level (search_stack);
  1591.  
  1592.   if (entry)
  1593.     {
  1594.       if (errstr)
  1595.     {
  1596.       tree error_string = my_build_string (errstr);
  1597.       /* Save error message with entry.  */
  1598.       TREE_TYPE (entry) = error_string;
  1599.     }
  1600.       else
  1601.     {
  1602.       /* Mark entry as having no error string.  */
  1603.       TREE_TYPE (entry) = NULL_TREE;
  1604.     }
  1605.     }
  1606.  
  1607.   if (errstr && protect)
  1608.     {
  1609.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (type));
  1610.       rvals = error_mark_node;
  1611.     }
  1612.  
  1613.   return rvals;
  1614. }
  1615.  
  1616. /* BREADTH-FIRST SEARCH ROUTINES.  */
  1617.  
  1618. /* Search a multiple inheritance hierarchy by breadth-first search.
  1619.  
  1620.    TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
  1621.    TESTFN is a function, which, if true, means that our condition has been met,
  1622.    and its return value should be returned.
  1623.    QFN, if non-NULL, is a predicate dictating whether the type should
  1624.    even be queued.  */
  1625.  
  1626. int
  1627. breadth_first_search (type, testfn, qfn)
  1628.      tree type;
  1629.      int (*testfn)();
  1630.      int (*qfn)();
  1631. {
  1632.   int head = 0, tail = 0;
  1633.   int rval = 0;
  1634.  
  1635.   search_stack = push_search_level (search_stack, &search_obstack);
  1636.  
  1637.   while (1)
  1638.     {
  1639.       int n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1640.       int i;
  1641.  
  1642.       /* Process and/or queue base types.  */
  1643.       for (i = 1; i <= n_baselinks; i++)
  1644.     if (CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0
  1645.         && (qfn == 0 || (*qfn) (type, i)))
  1646.       {
  1647.         CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 1;
  1648.         obstack_ptr_grow (&search_obstack, type);
  1649.         obstack_int_grow (&search_obstack, i);
  1650.         tail += 2;
  1651.         if (tail >= search_stack->limit)
  1652.           abort ();
  1653.       }
  1654.  
  1655.       /* Process head of queue, if one exists.  */
  1656.       if (head >= tail)
  1657.     {
  1658.       rval = 0;
  1659.       break;
  1660.     }
  1661.  
  1662.       type = search_stack->first[head++];
  1663.       i = (int)search_stack->first[head++];
  1664.       if (rval = (*testfn) (type, i))
  1665.     break;
  1666.       type = CLASSTYPE_BASECLASS (type, i);
  1667.     }
  1668.   {
  1669.     tree *tp = search_stack->first;
  1670.     tree *search_tail = tp + tail;
  1671.     while (tp < search_tail)
  1672.       {
  1673.     tree type = *tp++;
  1674.     int i = (int)(*tp++);
  1675.     CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 0;
  1676.       }
  1677.   }
  1678.  
  1679.   search_stack = pop_search_level (search_stack);
  1680.   return rval;
  1681. }
  1682.  
  1683. /* Functions to use in breadth first searches.  */
  1684. typedef tree (*pft)();
  1685. typedef int (*pfi)();
  1686.  
  1687. int tree_needs_constructor_p (type, i)
  1688.      tree type;
  1689. {
  1690.   tree basetype = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  1691.   return TYPE_NEEDS_CONSTRUCTOR (basetype);
  1692. }
  1693.  
  1694. static tree declarator;
  1695.  
  1696. static tree
  1697. get_virtuals_named_this (type, i)
  1698.      tree type;
  1699.      int i;
  1700. {
  1701.   tree basetype = i == 0? type : CLASSTYPE_BASECLASS (type, i);
  1702.   tree fields = lookup_fnfields (CLASSTYPE_AS_LIST (basetype), declarator, 0);
  1703.   tree field;
  1704.  
  1705.   if (fields == 0 || fields == error_mark_node)
  1706.     return 0;
  1707.  
  1708.   /* Get to the function decls, and return the first virtual function
  1709.      with this name, if there is one.  */
  1710.   while (fields)
  1711.     {
  1712.       for (field = TREE_VALUE (fields); field; field = TREE_CHAIN (field))
  1713.     if (DECL_VIRTUAL_P (field))
  1714.       return fields;
  1715.       fields = next_baselink (fields);
  1716.     }
  1717.   return NULL_TREE;
  1718. }
  1719.  
  1720. static tree get_virtual_destructor (type, i)
  1721.      tree type;
  1722.      int i;
  1723. {
  1724.   type = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  1725.   if (TYPE_HAS_DESTRUCTOR (type)
  1726.       && DECL_VIRTUAL_P (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
  1727.     return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  1728.   return 0;
  1729. }
  1730.  
  1731. int tree_has_any_destructor_p (type, i)
  1732.      tree type;
  1733.      int i;
  1734. {
  1735.   if (i == 0)
  1736.     return TYPE_NEEDS_DESTRUCTOR (type);
  1737.   return TYPE_NEEDS_DESTRUCTOR (CLASSTYPE_BASECLASS (type, i));
  1738. }
  1739.  
  1740. /* Given a class type TYPE, and a function decl FNDECL,
  1741.    look for the first function the TYPE's heirarchy which
  1742.    FNDECL could match as a virtual function.
  1743.  
  1744.    DTORP is nonzero if we are looking for a destructor.  Destructors
  1745.    need special treatment because they do not match by name.  */
  1746. tree
  1747. get_first_matching_virtual (type, fndecl, dtorp)
  1748.      tree type, fndecl;
  1749.      int dtorp;
  1750. {
  1751.   tree tmp = NULL_TREE;
  1752.  
  1753.   /* Breadth first search routines start searching basetypes
  1754.      of TYPE, so we must perform first ply of search here.  */
  1755.   if (dtorp)
  1756.     {
  1757.       if (tree_has_any_destructor_p (type, 0))
  1758.     tmp = get_virtual_destructor (type, 0);
  1759.  
  1760.       if (tmp)
  1761.     return tmp;
  1762.  
  1763.       tmp = (tree) breadth_first_search (type,
  1764.                      (pfi) get_virtual_destructor,
  1765.                      tree_has_any_destructor_p);
  1766.       return tmp;
  1767.     }
  1768.   else
  1769.     {
  1770.       tree drettype, dtypes, btypes;
  1771.       tree baselink, best = NULL_TREE;
  1772.  
  1773.       declarator = DECL_ORIGINAL_NAME (fndecl);
  1774.       if (DECL_VIRTUAL_P (declarator) == 0)
  1775.     return 0;
  1776.  
  1777.       drettype = TREE_TYPE (TREE_TYPE (fndecl));
  1778.       dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  1779.  
  1780.       for (baselink = get_virtuals_named_this (type, 0);
  1781.        baselink; baselink = next_baselink (baselink))
  1782.     {
  1783.       for (tmp = TREE_VALUE (baselink); tmp; tmp = TREE_CHAIN (tmp))
  1784.         {
  1785.           if (! DECL_VIRTUAL_P (tmp))
  1786.         continue;
  1787.           btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
  1788.           if ((TREE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
  1789.            == TREE_READONLY (TREE_TYPE (TREE_VALUE (dtypes))))
  1790.           && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 1))
  1791.         {
  1792.           if (IDENTIFIER_ERROR_LOCUS (DECL_NAME (fndecl)) == NULL_TREE
  1793.               && ! comptypes (TREE_TYPE (TREE_TYPE (tmp)), drettype, 1))
  1794.             {
  1795.               error_with_decl (fndecl, "conflicting return type specified for virtual function `%s'");
  1796.               SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (fndecl),
  1797.                           TYPE_METHOD_BASETYPE (fndecl));
  1798.             }
  1799.           break;
  1800.         }
  1801.         }
  1802.       if (tmp)
  1803.         {
  1804.           /* If this is ambiguous, we will warn about it later.  */
  1805.           if (best)
  1806.         {
  1807.           if (get_base_distance (TYPE_METHOD_BASETYPE (TREE_TYPE (best)),
  1808.                      TYPE_METHOD_BASETYPE (TREE_TYPE (tmp)), 0, 0) > 0)
  1809.             best = tmp;
  1810.         }
  1811.           else
  1812.         best = tmp;
  1813.         }
  1814.     }
  1815.       if (IDENTIFIER_ERROR_LOCUS (DECL_NAME (fndecl)) == NULL_TREE
  1816.       && best == NULL_TREE && warn_overloaded_virtual)
  1817.     {
  1818.       error_with_decl (fndecl, "conficting specification deriving virtual function `%s'");
  1819.       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (fndecl),
  1820.                       TYPE_METHOD_BASETYPE (fndecl));
  1821.     }
  1822.       return best;
  1823.     }
  1824. }
  1825.  
  1826. /* Return the list of virtual functions which are abstract in type TYPE.
  1827.    This information is cached, and so must be built on a
  1828.    non-temporary obstack.  */
  1829. tree
  1830. get_abstract_virtuals (type)
  1831.      tree type;
  1832. {
  1833.   /* For each layer of base class (i.e., the first base class, and each
  1834.      virtual base class from that one), modify the virtual function table
  1835.      of the derived class to contain the new virtual function.
  1836.      A class has as many vfields as it has virtual base classes (total).  */
  1837.   tree vslots, vbases, base, tmp;
  1838.   tree vfield = CLASSTYPE_VFIELD (type);
  1839.   tree fcontext = vfield ? DECL_FCONTEXT (vfield) : NULL_TREE;
  1840.   tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  1841.  
  1842.   for (vslots = CLASSTYPE_VFIELDS (type); vslots; vslots = TREE_CHAIN (vslots))
  1843.     {
  1844.       int normal;
  1845.  
  1846.       /* Find the right base class for this derived class, call it BASE.  */
  1847.       base = TREE_VALUE (vslots);
  1848.       if (base == type)
  1849.     continue;
  1850.  
  1851.       /* We call this case NORMAL iff this virtual function table
  1852.      pointer field has its storage reserved in this class.
  1853.      This is normally the case without virtual baseclasses
  1854.      or off-center multiple baseclasses.  */
  1855.       normal = (base == fcontext
  1856.         && (TREE_PURPOSE (vslots) == NULL_TREE
  1857.             || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vslots))));
  1858.  
  1859.       if (normal)
  1860.     tmp = TREE_CHAIN (CLASS_ASSOC_VIRTUALS (type));
  1861.       else
  1862.     {
  1863.       tree assoc = assoc_value (base, type);
  1864.       tmp = TREE_CHAIN (ASSOC_VIRTUALS (assoc));
  1865.     }
  1866.  
  1867.       while (tmp)
  1868.     {
  1869.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  1870.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  1871.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  1872.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  1873.       tmp = TREE_CHAIN (tmp);
  1874.     }
  1875.     }
  1876.   for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
  1877.     {
  1878.       if (! ASSOC_VIRTUALS (vbases));
  1879.     continue;
  1880.  
  1881.       base = TREE_TYPE (vbases);
  1882.       tmp = TREE_CHAIN (ASSOC_VIRTUALS (vbases));
  1883.       while (tmp)
  1884.     {
  1885.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  1886.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  1887.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  1888.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  1889.       tmp = TREE_CHAIN (tmp);
  1890.     }
  1891.     }
  1892.   return nreverse (abstract_virtuals);
  1893. }
  1894.  
  1895. /* For the type TYPE, return a list of member functions available from
  1896.    base classes with name NAME.  The TREE_VALUE of the list is a chain of
  1897.    member functions with name NAME.  The TREE_PURPOSE of the list is a
  1898.    basetype, or a list of base types (in reverse order) which were
  1899.    traversed to reach the chain of member functions.  If we reach a base
  1900.    type which provides a member function of name NAME, and which has at
  1901.    most one base type itself, then we can terminate the search.  */
  1902.  
  1903. tree
  1904. get_baselinks (type, name)
  1905.      tree type, name;
  1906. {
  1907.   tree hash_tree_cons ();
  1908.   int head = 0, tail = 0, index;
  1909.   tree rval = 0, nval = 0;
  1910.   tree basetypes = CLASSTYPE_AS_LIST (type);
  1911.  
  1912.   search_stack = push_search_level (search_stack, &search_obstack);
  1913.  
  1914.   while (1)
  1915.     {
  1916.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1917.  
  1918.       /* Process and/or queue base types.  */
  1919.       for (i = 1; i <= n_baselinks; i++)
  1920.     if (CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0)
  1921.       {
  1922.         tree btypes;
  1923.  
  1924.         btypes = hash_tree_cons (CLASSTYPE_VIA_PUBLIC (type, i),
  1925.                      CLASSTYPE_VIA_VIRTUAL (type, i),
  1926.                      NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1927.                      basetypes);
  1928.         CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 1;
  1929.         obstack_ptr_grow (&search_obstack, btypes);
  1930.  
  1931.         tail += 1;
  1932.         if (tail >= search_stack->limit)
  1933.           abort ();
  1934.       }
  1935.  
  1936.     dont_queue:
  1937.       /* Process head of queue, if one exists.  */
  1938.       if (head >= tail)
  1939.     break;
  1940.  
  1941.       basetypes = search_stack->first[head++];
  1942.       type = TREE_VALUE (basetypes);
  1943.       index = lookup_fnfields_1 (type, name);
  1944.       if (index >= 0)
  1945.     {
  1946.       nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1947.       rval = hash_tree_cons (0, 0, basetypes, nval, rval);
  1948.       if (CLASSTYPE_N_BASECLASSES (type) <= 1)
  1949.         {
  1950.           if (CLASSTYPE_BASELINK_VEC (type))
  1951.         TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1952.           goto dont_queue;
  1953.         }
  1954.     }
  1955.       nval = NULL_TREE;
  1956.     }
  1957.   {
  1958.     tree *tp = search_stack->first;
  1959.     tree *search_tail = tp + tail;
  1960.  
  1961.     while (tp < search_tail)
  1962.       {
  1963.     CLASSTYPE_MARKED (TREE_VALUE (*tp++)) = 0;
  1964.       }
  1965.   }
  1966.   search_stack = pop_search_level (search_stack);
  1967.   return rval;
  1968. }
  1969.  
  1970. tree
  1971. next_baselink (baselink)
  1972.      tree baselink;
  1973. {
  1974.   tree tmp = TREE_TYPE (baselink);
  1975.   baselink = TREE_CHAIN (baselink);
  1976.   while (tmp)
  1977.     {
  1978.       /* @@ does not yet add previous base types.  */
  1979.       baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
  1980.                 baselink);
  1981.       TREE_TYPE (baselink) = TREE_TYPE (tmp);
  1982.       tmp = TREE_CHAIN (tmp);
  1983.     }
  1984.   return baselink;
  1985. }
  1986.  
  1987. /* DEPTH-FIRST SEARCH ROUTINES.  */
  1988.  
  1989. /* Assign unique numbers to _CLASSTYPE members of the lattice
  1990.    specified by TYPE.  The root nodes are marked first; the nodes
  1991.    are marked depth-fisrt, left-right.  */
  1992.  
  1993. static int cid;
  1994.  
  1995. /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
  1996.    Relation yields 1 if C1 <= C2, 0 otherwise.  */
  1997. typedef char mi_boolean;
  1998. static mi_boolean *mi_matrix;
  1999.  
  2000. /* Type for which this matrix is defined.  */
  2001. static tree mi_type;
  2002.  
  2003. /* Size of the matrix for indexing purposes.  */
  2004. static int mi_size;
  2005.  
  2006. /* Return nonzero if class C2 derives from class C1.  */
  2007. #define DERIVES_FROM(C1, C2)    \
  2008.   ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
  2009. #define DERIVES_FROM_STAR(C)    \
  2010.   (mi_matrix+(CLASSTYPE_CID (C)-1))
  2011.  
  2012. /* The main function which implements depth first search.  */
  2013. static void
  2014. dfs_walk (type, fn, qfn)
  2015.      tree type;
  2016.      void (*fn)();
  2017.      int (*qfn)();
  2018. {
  2019.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2020.  
  2021.   for (i = 1; i <= n_baselinks; i++)
  2022.     if ((*qfn)(CLASSTYPE_BASECLASS (type, i)))
  2023.       {
  2024.     dfs_walk (CLASSTYPE_BASECLASS (type, i), fn, qfn);
  2025.       }
  2026.  
  2027.   fn (type);
  2028. }
  2029.  
  2030. /* Predicate functions which serve for dfs_walk.  */
  2031. static int numberedp (type) tree type;
  2032. { return CLASSTYPE_CID (type); }
  2033. static int unnumberedp (type) tree type;
  2034. { return CLASSTYPE_CID (type) == 0; }
  2035.  
  2036. static int markedp (type) tree type;
  2037. { return CLASSTYPE_MARKED (type); }
  2038. static int bfs_markedp (type, i) tree type; int i;
  2039. { return CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)); }
  2040. static int unmarkedp (type) tree type;
  2041. { return CLASSTYPE_MARKED (type) == 0; }
  2042. static int bfs_unmarkedp (type, i) tree type; int i;
  2043. { return CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2044. static int marked2p (type) tree type;
  2045. { return CLASSTYPE_MARKED2 (type); }
  2046. static int bfs_marked2p (type, i) tree type; int i;
  2047. { return CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)); }
  2048. static int unmarked2p (type) tree type;
  2049. { return CLASSTYPE_MARKED2 (type) == 0; }
  2050. static int bfs_unmarked2p (type, i) tree type; int i;
  2051. { return CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2052. static int marked3p (type) tree type;
  2053. { return CLASSTYPE_MARKED3 (type); }
  2054. static int bfs_marked3p (type, i) tree type; int i;
  2055. { return CLASSTYPE_MARKED3 (CLASSTYPE_BASECLASS (type, i)); }
  2056. static int unmarked3p (type) tree type;
  2057. { return CLASSTYPE_MARKED3 (type) == 0; }
  2058. static int bfs_unmarked3p (type, i) tree type; int i;
  2059. { return CLASSTYPE_MARKED3 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2060. static int marked4p (type) tree type;
  2061. { return CLASSTYPE_MARKED4 (type); }
  2062. static int bfs_marked4p (type, i) tree type; int i;
  2063. { return CLASSTYPE_MARKED4 (CLASSTYPE_BASECLASS (type, i)); }
  2064. static int unmarked4p (type) tree type;
  2065. { return CLASSTYPE_MARKED4 (type) == 0; }
  2066. static int bfs_unmarked4p (type, i) tree type; int i;
  2067. { return CLASSTYPE_MARKED4 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2068.  
  2069. static int dfs_search_slot_nonempty_p (type) tree type;
  2070. { return CLASSTYPE_SEARCH_SLOT (type) != 0; }
  2071.  
  2072.  
  2073. /* The worker functions for `dfs_walk'.  These do not need to
  2074.    test anything (vis a vis marking) if they are paired with
  2075.    a predicate function (above).  */
  2076.  
  2077. /* Assign each type within the lattice a number which is unique
  2078.    in the lattice.  The first number assigned is 1.  */
  2079.  
  2080. static void
  2081. dfs_number (type)
  2082.      tree type;
  2083. {
  2084.   CLASSTYPE_CID (type) = ++cid;
  2085. }
  2086.  
  2087. static void
  2088. dfs_unnumber (type)
  2089.      tree type;
  2090. {
  2091.   CLASSTYPE_CID (type) = 0;
  2092. }
  2093.  
  2094. static void
  2095. dfs_mark (type) tree type;
  2096. { CLASSTYPE_MARKED (type) = 1; }
  2097.  
  2098. static void
  2099. dfs_unmark (type) tree type;
  2100. { CLASSTYPE_MARKED (type) = 0; }
  2101.  
  2102. static void
  2103. dfs_mark2 (type) tree type;
  2104. { CLASSTYPE_MARKED2 (type) = 1; }
  2105.  
  2106. static void
  2107. dfs_unmark2 (type) tree type;
  2108. { CLASSTYPE_MARKED2 (type) = 0; }
  2109.  
  2110. static void
  2111. dfs_mark3 (type) tree type;
  2112. { CLASSTYPE_MARKED3 (type) = 1; }
  2113.  
  2114. static void
  2115. dfs_unmark3 (type) tree type;
  2116. { CLASSTYPE_MARKED3 (type) = 0; }
  2117.  
  2118. static void
  2119. dfs_mark4 (type) tree type;
  2120. { CLASSTYPE_MARKED4 (type) = 1; }
  2121.  
  2122. static void
  2123. dfs_unmark4 (type) tree type;
  2124. { CLASSTYPE_MARKED4 (type) = 0; }
  2125.  
  2126. static void
  2127. dfs_unmark12 (type) tree type;
  2128. { CLASSTYPE_MARKED (type) = 0;
  2129.   CLASSTYPE_MARKED2 (type) = 0; }
  2130.  
  2131. static void
  2132. dfs_unmark34 (type) tree type;
  2133. { CLASSTYPE_MARKED3 (type) = 0;
  2134.   CLASSTYPE_MARKED4 (type) = 0; }
  2135.  
  2136. static tree
  2137. dfs_clear_search_slot (type) tree type;
  2138. { CLASSTYPE_SEARCH_SLOT (type) = 0; }
  2139.  
  2140. static tree vbase_types;
  2141. static tree vbase_decl, vbase_decl_ptr, vbase_char_star;
  2142. static tree vbase_init_result;
  2143.  
  2144. static void
  2145. dfs_find_vbases (type)
  2146.      tree type;
  2147. {
  2148.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2149.  
  2150.   for (i = n_baselinks; i > 0; i--)
  2151.     if (CLASSTYPE_VIA_VIRTUAL (type, i)
  2152.     && CLASSTYPE_SEARCH_SLOT (CLASSTYPE_BASECLASS (type, i)) == 0)
  2153.       {
  2154.     tree vbase = CLASSTYPE_BASECLASS (type, i);
  2155.     /* ??? ASSOC_VALUE and TREE_VALUE must be the same for this to work.  */
  2156.     tree assoc = value_member (TYPE_MAIN_VARIANT (vbase), vbase_types);
  2157.  
  2158.     CLASSTYPE_SEARCH_SLOT (vbase)
  2159.       = build (PLUS_EXPR, TYPE_POINTER_TO (vbase),
  2160.            vbase_char_star, ASSOC_OFFSET (assoc));
  2161.       }
  2162.   CLASSTYPE_MARKED3 (type) = 1;
  2163.   CLASSTYPE_MARKED4 (type) = 1;
  2164. }
  2165.  
  2166. static void
  2167. dfs_init_vbase_pointers (type)
  2168.      tree type;
  2169. {
  2170.   tree fields = TYPE_FIELDS (type);
  2171.   tree this_vbase_ptr = convert_pointer_to (type, vbase_decl_ptr);
  2172.   while (fields && DECL_NAME (fields)
  2173.      && VBASE_NAME_P (DECL_NAME (fields)))
  2174.     {
  2175.       tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
  2176.             build_indirect_ref (this_vbase_ptr, 0), fields);
  2177.       tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
  2178.       vbase_init_result = tree_cons (TREE_TYPE (TREE_TYPE (fields)),
  2179.                      build_modify_expr (ref, NOP_EXPR, init),
  2180.                      vbase_init_result);
  2181.       fields = TREE_CHAIN (fields);
  2182.     }
  2183.   CLASSTYPE_MARKED3 (type) = 0;
  2184. }
  2185.  
  2186. /* Sometimes this needs to clear both 3 and 4.  Other times,
  2187.    just 4, but optimizer should make both with equal efficiency
  2188.    (though it does not currently).  */
  2189. static void
  2190. dfs_clear_vbase_slots (type)
  2191.      tree type;
  2192. {
  2193.   CLASSTYPE_SEARCH_SLOT (type) = 0;
  2194.   CLASSTYPE_MARKED3 (type) = 0;
  2195.   CLASSTYPE_MARKED4 (type) = 0;
  2196. }
  2197.  
  2198. tree
  2199. init_vbase_pointers (type, decl_ptr)
  2200.      tree type;
  2201.      tree decl_ptr;
  2202. {
  2203.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2204.     {
  2205.       int old_flag = flag_this_is_variable;
  2206.       flag_this_is_variable = 0;
  2207.       vbase_types = CLASSTYPE_VBASECLASSES (type);
  2208.       vbase_decl_ptr = decl_ptr;
  2209.       vbase_decl = build_indirect_ref (decl_ptr, 0);
  2210.       vbase_char_star = build1 (NOP_EXPR, ptr_type_node, decl_ptr);
  2211.       vbase_init_result = NULL_TREE;
  2212. #ifdef sparc
  2213.       expand_asm_operands (build_string (32, "! start of vbase initialization"), 0, 0, 0, 0, input_filename, lineno);
  2214. #endif
  2215.       dfs_walk (type, dfs_find_vbases, unmarked3p);
  2216.       dfs_walk (type, dfs_init_vbase_pointers, marked3p);
  2217.       dfs_walk (type, dfs_clear_vbase_slots, marked4p);
  2218.       flag_this_is_variable = old_flag;
  2219.       return vbase_init_result;
  2220.     }
  2221.   return 0;
  2222. }
  2223.  
  2224. tree
  2225. build_vbase_vtables_init (for_type, type, true_exp, decl_ptr)
  2226.      tree for_type, type;
  2227.      tree true_exp, decl_ptr;
  2228. {
  2229.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2230.     {
  2231.       int old_flag = flag_this_is_variable;
  2232.       tree vtable_init_result = NULL_TREE;
  2233.       tree vbases = CLASSTYPE_VBASECLASSES (type);
  2234.  
  2235.       vbase_types = CLASSTYPE_VBASECLASSES (for_type);
  2236.       vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
  2237.       vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, 0);
  2238.       vbase_char_star = build1 (NOP_EXPR, ptr_type_node, vbase_decl_ptr);
  2239. #ifdef sparc
  2240.       expand_asm_operands (build_string (32, "! start of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
  2241. #endif
  2242.       flag_this_is_variable = 0;
  2243.  
  2244.       /* This is an object of type IN_TYPE,  */
  2245.       dfs_walk (for_type, dfs_find_vbases, unmarked4p);
  2246.       /* Initialized with vtables of type TYPE.  */
  2247.  
  2248.       while (vbases)
  2249.     {
  2250.       tree basetype = get_base_type (ASSOC_VALUE (vbases), type, 0);
  2251.       /* This time through, not every class's vtable
  2252.          is going to be initialized.  That is, we only initialize
  2253.          the "last" vtable pointer.  */
  2254.       if (basetype
  2255.           && CLASSTYPE_VSIZE (basetype)
  2256.           && TYPE_MAIN_VARIANT (basetype) == ASSOC_VALUE (vbases)
  2257.           && CLASSTYPE_SEARCH_SLOT (basetype))
  2258.         {
  2259.           tree ref = build_vfield_ref (build_indirect_ref (CLASSTYPE_SEARCH_SLOT (basetype)),
  2260.                        basetype);
  2261.           tree vtbl = ASSOC_VTABLE (vbases);
  2262.           tree init = build_unary_op (ADDR_EXPR, vtbl, 0);
  2263.           TREE_USED (vtbl) = 1;
  2264.           vtable_init_result = tree_cons (NULL_TREE, build_modify_expr (ref, NOP_EXPR, init),
  2265.                           vtable_init_result);
  2266.         }
  2267.       vbases = TREE_CHAIN (vbases);
  2268.     }
  2269.  
  2270.       dfs_walk (type, dfs_clear_vbase_slots, marked4p);
  2271.  
  2272.       flag_this_is_variable = old_flag;
  2273.       if (vtable_init_result)
  2274.     return build_compound_expr (vtable_init_result);
  2275.     }
  2276.   return error_mark_node;
  2277. }
  2278.  
  2279. tree
  2280. clear_search_slots (type)
  2281.      tree type;
  2282. {
  2283.   dfs_walk (type, dfs_clear_search_slot, dfs_search_slot_nonempty_p);
  2284. }
  2285.  
  2286. static void
  2287. dfs_get_vbase_types (type)
  2288.      tree type;
  2289. {
  2290.   int i;
  2291.   tree these_vbase_types = CLASSTYPE_VBASECLASSES (type);
  2292.   tree basetype;
  2293.  
  2294.   if (these_vbase_types)
  2295.     {
  2296.       while (these_vbase_types)
  2297.     {
  2298.       basetype = ASSOC_TYPE (these_vbase_types);
  2299.       if (! CLASSTYPE_MARKED2 (basetype))
  2300.         {
  2301.           vbase_types = make_assoc (integer_zero_node,
  2302.                     basetype,
  2303.                     CLASS_ASSOC_VTABLE (basetype),
  2304.                     CLASS_ASSOC_VIRTUALS (basetype),
  2305.                     vbase_types);
  2306.           CLASSTYPE_MARKED2 (basetype) = 1;
  2307.         }
  2308.       these_vbase_types = TREE_CHAIN (these_vbase_types);
  2309.     }
  2310.     }
  2311.   else for (i = CLASSTYPE_N_BASECLASSES (type); i > 0; i--)
  2312.     {
  2313.       basetype = CLASSTYPE_BASECLASS (type, i);
  2314.       if (CLASSTYPE_VIA_VIRTUAL (type, i) && ! CLASSTYPE_MARKED2 (basetype))
  2315.     {
  2316.       vbase_types = make_assoc (integer_zero_node,
  2317.                     basetype,
  2318.                     CLASS_ASSOC_VTABLE (basetype),
  2319.                     CLASS_ASSOC_VIRTUALS (basetype),
  2320.                     vbase_types);
  2321.       CLASSTYPE_MARKED2 (basetype) = 1;
  2322.     }
  2323.     }
  2324.   CLASSTYPE_MARKED (type) = 1;
  2325. }
  2326.  
  2327. /* Some virtual baseclasses might be virtual baseclasses for
  2328.    other virtual baseclasses.  We sort the virtual baseclasses
  2329.    topologically: in the list returned, the first virtual base
  2330.    classes have no virtual baseclasses themselves, and any entry
  2331.    on the list has no dependency on virtual base classes later in the
  2332.    list.  */
  2333. tree
  2334. get_vbase_types (type)
  2335.      tree type;
  2336. {
  2337.   tree ordered_vbase_types = NULL_TREE, prev, next;
  2338.   tree vbases;
  2339.  
  2340.   vbase_types = NULL_TREE;
  2341.   dfs_walk (type, dfs_get_vbase_types, unmarkedp);
  2342.   dfs_walk (type, dfs_unmark, markedp);
  2343.  
  2344.   while (vbase_types)
  2345.     {
  2346.       /* Now sort these types.  This is essentially a bubble merge.  */
  2347.  
  2348.       /* Farm out virtual baseclasses which have no marked ancestors.  */
  2349.       for (vbases = vbase_types, prev = NULL_TREE;
  2350.        vbases; vbases = next)
  2351.     {
  2352.       next = TREE_CHAIN (vbases);
  2353.       if (! TYPE_USES_VIRTUAL_BASECLASSES (ASSOC_TYPE (vbases))
  2354.           || CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) == 0)
  2355.         {
  2356.           if (prev)
  2357.         TREE_CHAIN (prev) = TREE_CHAIN (vbases);
  2358.           else
  2359.         vbase_types = TREE_CHAIN (vbases);
  2360.           TREE_CHAIN (vbases) = NULL_TREE;
  2361.           ordered_vbase_types = chainon (ordered_vbase_types, vbases);
  2362.           CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) = 0;
  2363.         }
  2364.       else
  2365.         prev = vbases;
  2366.     }
  2367.  
  2368.       /* Now unmark types all of whose ancestors are now on the
  2369.      `ordered_vbase_types' list.  */
  2370.       for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
  2371.     {
  2372.       /* If all our virtual baseclasses are unmarked, ok.  */
  2373.       tree t = CLASSTYPE_VBASECLASSES (ASSOC_VALUE (vbases));
  2374.       while (t && (CLASSTYPE_MARKED2 (ASSOC_TYPE (t)) == 0
  2375.                || !TYPE_USES_VIRTUAL_BASECLASSES (ASSOC_TYPE (t))))
  2376.         t = TREE_CHAIN (t);
  2377.       if (t == NULL_TREE)
  2378.         CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) = 0;
  2379.     }
  2380.     }
  2381.  
  2382.   return ordered_vbase_types;
  2383. }
  2384.  
  2385. static void
  2386. dfs_record_inheritance (type)
  2387.      tree type;
  2388. {
  2389.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2390.   mi_boolean *derived_row = DERIVES_FROM_STAR (type);
  2391.  
  2392.   for (i = n_baselinks; i > 0; i--)
  2393.     {
  2394.       int j;
  2395.       tree baseclass = CLASSTYPE_BASECLASS (type, i);
  2396.       mi_boolean *base_row = DERIVES_FROM_STAR (baseclass);
  2397.  
  2398.       /* Don't search if there's nothing there!  MI_SIZE can be
  2399.      zero as a result of parse errors.  */
  2400.       if (CLASSTYPE_N_BASECLASSES (baseclass) && mi_size > 0)
  2401.     for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
  2402.       derived_row[j] |= base_row[j];
  2403.       DERIVES_FROM (baseclass, type) = 1;
  2404.     }
  2405.  
  2406.   CLASSTYPE_MARKED (type) = 1;
  2407. }
  2408.  
  2409. /* Given a _CLASSTYPE node in a multiple inheritance lattice,
  2410.    convert the lattice into a simple relation such that,
  2411.    given to CIDs, C1 and C2, one can determine if C1 <= C2
  2412.    or C2 <= C1 or C1 <> C2.
  2413.  
  2414.    Once constructed, we walk the lattice depth fisrt,
  2415.    applying various functions to elements as they are encountered.
  2416.  
  2417.    We use malloc here, in case we want to randomly free these tables.  */
  2418.  
  2419. #define SAVE_MI_MATRIX
  2420.  
  2421. void
  2422. build_mi_matrix (type)
  2423.      tree type;
  2424. {
  2425.   cid = 0;
  2426.  
  2427. #ifdef SAVE_MI_MATRIX
  2428.   if (CLASSTYPE_MI_MATRIX (type))
  2429.     {
  2430.       mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2431.       mi_matrix = CLASSTYPE_MI_MATRIX (type);
  2432.       mi_type = type;
  2433.       dfs_walk (type, dfs_number, unnumberedp);
  2434.       return;
  2435.     }
  2436. #endif
  2437.  
  2438.   mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2439.   mi_matrix = (char *)malloc ((mi_size+1) * (mi_size+1));
  2440.   mi_type = type;
  2441.   bzero (mi_matrix, mi_size * mi_size);
  2442.   dfs_walk (type, dfs_number, unnumberedp);
  2443.   dfs_walk (type, dfs_record_inheritance, unmarkedp);
  2444.   dfs_walk (type, dfs_unmark, markedp);
  2445. }
  2446.  
  2447. void
  2448. free_mi_matrix ()
  2449. {
  2450.   dfs_walk (mi_type, dfs_unnumber, numberedp);
  2451.  
  2452. #ifdef SAVE_MI_MATRIX
  2453.   CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
  2454. #else
  2455.   free (mi_matrix);
  2456.   mi_size = 0;
  2457.   cid = 0;
  2458. #endif
  2459. }
  2460.  
  2461. /* Local variables for detecting ambiguities of virtual functions
  2462.    when two or more classes are joined at a multiple inheritance
  2463.    seam.  */
  2464. typedef tree mi_ventry[3];
  2465. static mi_ventry *mi_vmatrix;
  2466. static int *mi_vmax;
  2467. static int mi_vrows, mi_vcols;
  2468. #define MI_VMATRIX(ROW,COL) ((mi_vmatrix + (ROW)*mi_vcols)[COL])
  2469.  
  2470. /* Build a table of virtual functions for a multiple-inheritance
  2471.    structure.  Here, there are N base classes, and at most
  2472.    M entries per class.
  2473.  
  2474.    This function does nothing if N is 0 or 1.  */
  2475. void
  2476. build_mi_virtuals (rows, cols)
  2477.      int rows, cols;
  2478. {
  2479.   if (rows < 2)
  2480.     return;
  2481.   mi_vrows = rows;
  2482.   mi_vcols = cols;
  2483.   mi_vmatrix = (mi_ventry *)malloc ((rows+1) * cols * sizeof (mi_ventry));
  2484.   mi_vmax = (int *)malloc ((rows+1) * sizeof (int));
  2485.  
  2486.   bzero (mi_vmax, rows * sizeof (int));
  2487.  
  2488.   /* Row indicies start at 1, so adjust this.  */
  2489.   mi_vmatrix -= cols;
  2490.   mi_vmax -= 1;
  2491. }
  2492.  
  2493. /* Comparison function for ordering virtual function table entries.  */
  2494. static int
  2495. rank_mi_virtuals (v1, v2)
  2496.      mi_ventry *v1, *v2;
  2497. {
  2498.   tree p1, p2;
  2499.   int i;
  2500.  
  2501.   i = (TREE_UID (DECL_ORIGINAL_NAME ((*v1)[0]))
  2502.        - TREE_UID (DECL_ORIGINAL_NAME ((*v2)[0])));
  2503.   if (i)
  2504.     return i;
  2505.   p1 = (*v1)[1];
  2506.   p2 = (*v2)[1];
  2507.  
  2508.   if (p1 == p2)
  2509.     return 0;
  2510.  
  2511.   while (p1 && p2)
  2512.     {
  2513.       i = (TREE_UID (TREE_VALUE (p1))
  2514.        - TREE_UID (TREE_VALUE (p2)));
  2515.       if (i)
  2516.     return i;
  2517.  
  2518.       if (TREE_CHAIN (p1))
  2519.     {
  2520.       if (! TREE_CHAIN (p2))
  2521.         return 1;
  2522.       p1 = TREE_CHAIN (p1);
  2523.       p2 = TREE_CHAIN (p2);
  2524.     }
  2525.       else if (TREE_CHAIN (p2))
  2526.     return -1;
  2527.       else
  2528.     {
  2529.       /* When matches of argument lists occur, pick lowest
  2530.          TREE_UID to keep searching time to a minimum on
  2531.          later passes--like hashing, only different.
  2532.          *MUST BE STABLE*.  */
  2533.       if (TREE_UID ((*v2)[1]) < TREE_UID ((*v1)[1]))
  2534.         (*v1)[1] = (*v2)[1];
  2535.       else
  2536.         (*v2)[1] = (*v1)[1];
  2537.       return 0;
  2538.     }
  2539.     }
  2540.   return 0;
  2541. }
  2542.  
  2543. /* Install the virtuals functions got from the initializer VIRTUALS to
  2544.    the table at index ROW.  */
  2545. void
  2546. add_mi_virtuals (row, virtuals)
  2547.      int row;
  2548.      tree virtuals;
  2549. {
  2550.   int col = 0;
  2551.  
  2552.   if (mi_vmatrix == 0)
  2553.     return;
  2554.   while (virtuals)
  2555.     {
  2556.       tree decl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
  2557.       MI_VMATRIX (row, col)[0] = decl;
  2558.       MI_VMATRIX (row, col)[1] = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
  2559.       MI_VMATRIX (row, col)[2] = TREE_VALUE (virtuals);
  2560.       virtuals = TREE_CHAIN (virtuals);
  2561.       col += 1;
  2562.     }
  2563.   mi_vmax[row] = col;
  2564.  
  2565.   qsort (mi_vmatrix + row * mi_vcols,
  2566.      col,
  2567.      sizeof (mi_ventry),
  2568.      rank_mi_virtuals);
  2569. }
  2570.  
  2571. /* If joining two types results in an ambiguity in the virtual
  2572.    function table, report such here.  */
  2573. void
  2574. report_ambiguous_mi_virtuals (rows, type)
  2575.      int rows;
  2576.      tree type;
  2577. {
  2578.   int *mi_vmin;
  2579.   int row1, col1, row, col;
  2580.  
  2581.   if (mi_vmatrix == 0)
  2582.     return;
  2583.  
  2584.   /* Now virtuals are all sorted, so we merge to find ambiguous cases.  */
  2585.   mi_vmin = (int *)alloca ((rows+1) * sizeof (int));
  2586.   bzero (mi_vmin, rows * sizeof (int));
  2587.  
  2588.   /* adjust.  */
  2589.   mi_vmin -= 1;
  2590.  
  2591.   /* For each base class with virtual functions (and this includes views
  2592.      of the virtual baseclasses from different base classes), see that
  2593.      each virtual function in that base class has a unique meet.
  2594.  
  2595.      When the column loop is finished, THIS_DECL is in fact the meet.
  2596.      If that value does not appear in the virtual function table for
  2597.      the row, install it.  This happens when that virtual function comes
  2598.      from a virtual baseclass, or a non-leftmost baseclass.  */
  2599.      
  2600.   for (row1 = 1; row1 < rows; row1++)
  2601.     {
  2602.       tree this_decl = 0;
  2603.  
  2604.       for (col1 = mi_vmax[row1]-1; col1 >= mi_vmin[row1]; col1--)
  2605.     {
  2606.       tree these_args = MI_VMATRIX (row1, col1)[1];
  2607.       tree this_context;
  2608.  
  2609.       this_decl = MI_VMATRIX (row1, col1)[0];
  2610.       this_context = DECL_CONTEXT (this_decl);
  2611.  
  2612.       if (this_context != type)
  2613.         this_context = get_base_type (this_context, type, 0);
  2614.  
  2615.       for (row = row1+1; row <= rows; row++)
  2616.         for (col = mi_vmax[row]-1; col >= mi_vmin[row]; col--)
  2617.           {
  2618.         mi_ventry this_entry;
  2619.  
  2620.         if (MI_VMATRIX (row, col)[0] == 0)
  2621.           continue;
  2622.  
  2623.         this_entry[0] = this_decl;
  2624.         this_entry[1] = these_args;
  2625.         this_entry[2] = MI_VMATRIX (row1, col1)[2];
  2626.         if (rank_mi_virtuals (&this_entry,
  2627.                       &MI_VMATRIX (row, col)) == 0)
  2628.           {
  2629.             /* They are equal.  There are four possibilities:
  2630.                
  2631.                (1) Derived class is defining this virtual function.
  2632.                (2) Two paths to the same virtual function in the
  2633.                same base class.
  2634.                (3) A path to a virtual function declared in one base
  2635.                class, and another path to a virtual function in a
  2636.                base class of the base class.
  2637.                (4) Two paths to the same virtual function in different
  2638.                base classes.
  2639.                
  2640.                The first three cases are ok (non-ambiguous).  */
  2641.  
  2642.             tree that_context, tmp;
  2643.             int this_before_that;
  2644.  
  2645.             if (type == this_context)
  2646.               /* case 1.  */
  2647.               goto ok;
  2648.             that_context = get_base_type (DECL_CONTEXT (MI_VMATRIX (row, col)[0]), type, 0);
  2649.             if (that_context == this_context)
  2650.               /* case 2.  */
  2651.               goto ok;
  2652.             if (that_context != NULL_TREE)
  2653.               {
  2654.             tmp = get_base_type (that_context, this_context, 0);
  2655.             this_before_that = (that_context != tmp);
  2656.             if (this_before_that == 0)
  2657.               /* case 3a.  */
  2658.               goto ok;
  2659.             tmp = get_base_type (this_context, that_context, 0);
  2660.             this_before_that = (this_context == tmp);
  2661.             if (this_before_that != 0)
  2662.               /* case 3b.  */
  2663.               goto ok;
  2664.  
  2665.             /* case 4.  */
  2666.             error_with_decl (MI_VMATRIX (row, col)[0], "ambiguous virtual function `%s'");
  2667.             error_with_decl (this_decl, "ambiguating function `%s' (joined by type `%s')", IDENTIFIER_POINTER (current_class_name));
  2668.               }
  2669.           ok:
  2670.             MI_VMATRIX (row, col)[0] = 0;
  2671.  
  2672.             /* Let zeros propagate.  */
  2673.             if (col == mi_vmax[row]-1)
  2674.               {
  2675.             int i = col;
  2676.             while (i >= mi_vmin[row]
  2677.                    && MI_VMATRIX (row, i)[0] == 0)
  2678.               i--;
  2679.             mi_vmax[row] = i;
  2680.               }
  2681.             else if (col == mi_vmin[row])
  2682.               {
  2683.             int i = col;
  2684.             while (i < mi_vmax[row]
  2685.                    && MI_VMATRIX (row, i)[0] == 0)
  2686.               i++;
  2687.             mi_vmin[row] = i;
  2688.               }
  2689.           }
  2690.           }
  2691.     }
  2692.     }
  2693.   free (mi_vmatrix + mi_vcols);
  2694.   mi_vmatrix = 0;
  2695.   free (mi_vmax + 1);
  2696.   mi_vmax = 0;
  2697. }
  2698.  
  2699. /* Subroutines of push_class_decls ().  */
  2700.  
  2701. /* Add the instance variables which this class contributed to the
  2702.    current class binding contour.  When a redefinition occurs,
  2703.    if the redefinition is strictly within a single inheritance path,
  2704.    we just overwrite (in the case of a data field) or
  2705.    cons (in the case of a member function) the old declaration with
  2706.    the new.  If the fields are not within a single inheritance path,
  2707.    we must cons them in either case.  */
  2708.  
  2709. static void
  2710. dfs_pushdecls (type)
  2711.      tree type;
  2712. {
  2713.   tree fields, *methods, *end;
  2714.   tree method_vec;
  2715.  
  2716.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  2717.     {
  2718.       /* Unmark so that if we are in a constructor, and then find that
  2719.      this field was initialized by a base initializer,
  2720.      we can emit an error message.  */
  2721.       if (TREE_CODE (fields) == FIELD_DECL)
  2722.     TREE_USED (fields) = 0;
  2723.  
  2724.       if (DECL_ANON_UNION_ELEM (fields))
  2725.     {
  2726.       dfs_pushdecls (TREE_TYPE (fields));
  2727.       continue;
  2728.     }
  2729.       if (TREE_CODE (fields) != TYPE_DECL)
  2730.     {
  2731.       TREE_FIELD_PUBLIC (fields) = 0;
  2732.       TREE_FIELD_PROTECTED (fields) = 0;
  2733.       TREE_FIELD_PRIVATE (fields) = 0;
  2734.     }
  2735.  
  2736.       if (DECL_NAME (fields))
  2737.     {
  2738.       tree value = IDENTIFIER_CLASS_VALUE (DECL_NAME (fields));
  2739.       if (value)
  2740.         {
  2741.           /* Possible ambiguity.  If its defining type(s)
  2742.          is (are all) derived from us, no problem.  */
  2743.  
  2744.           if (TREE_CODE (value) != TREE_LIST)
  2745.         {
  2746.           if (DERIVES_FROM (DECL_FIELD_CONTEXT (value), type))
  2747.             value = fields;
  2748.           else
  2749.             value = tree_cons (NULL_TREE, fields,
  2750.                        build_tree_list (NULL_TREE, value));
  2751.         }
  2752.           else
  2753.         {
  2754.           /* All children may derive from us, in which case
  2755.              there is no problem.  Otherwise, we have to
  2756.              keep lists around of what the ambiguities might be.  */
  2757.           tree values;
  2758.           int problem = 0;
  2759.  
  2760.           for (values = value; values; values = TREE_CHAIN (values))
  2761.             {
  2762.               tree sub_values = TREE_VALUE (values);
  2763.               if (TREE_CODE (sub_values) == TREE_LIST)
  2764.             {
  2765.               for (; sub_values; sub_values = TREE_CHAIN (sub_values))
  2766.                 if (! DERIVES_FROM (DECL_FIELD_CONTEXT (TREE_VALUE (sub_values)), type))
  2767.                   {
  2768.                 value = tree_cons (NULL_TREE, TREE_VALUE (values), value);
  2769.                 problem = 1;
  2770.                 break;
  2771.                   }
  2772.             }
  2773.               else
  2774.             {
  2775.               if (! DERIVES_FROM (DECL_FIELD_CONTEXT (sub_values), type))
  2776.                 {
  2777.                   value = tree_cons (NULL_TREE, values, value);
  2778.                   problem = 1;
  2779.                   break;
  2780.                 }
  2781.             }
  2782.             }
  2783.           if (! problem) value = fields;
  2784.         }
  2785.  
  2786.           /* Mark this as a potentially ambiguous member.  */
  2787.           if (TREE_CODE (value) == TREE_LIST)
  2788.         {
  2789.           /* Leaving TREE_TYPE blank is intentional.
  2790.              We cannot use `error_mark_node' (lookup_name)
  2791.              or `unknown_type_node' (all member functions use this).  */
  2792.           TREE_NONLOCAL (value) = 1;
  2793.         }
  2794.  
  2795.           IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = value;
  2796.         }
  2797.       else IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = fields;
  2798.     }
  2799.     }
  2800.  
  2801.   method_vec = CLASSTYPE_METHOD_VEC (type);
  2802.   if (method_vec != 0)
  2803.     {
  2804.       /* Farm out constructors and destructors.  */
  2805.       methods = &TREE_VEC_ELT (method_vec, 1);
  2806.       end = TREE_VEC_END (method_vec);
  2807.  
  2808.       /* This does not work for multiple inheritance yet.  */
  2809.       while (methods != end)
  2810.     {
  2811.       /* This will cause lookup_name to return a pointer
  2812.          to the tree_list of possible methods of this name.
  2813.          If the order is a problem, we can nreverse them.  */
  2814.       tree tmp;
  2815.       tree old = IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods));
  2816.  
  2817.       if (old && TREE_CODE (old) == TREE_LIST)
  2818.         tmp = tree_cons (DECL_ORIGINAL_NAME (*methods), *methods, old);
  2819.       else
  2820.         {
  2821.           /* Only complain if we shadow something we can access.  */
  2822.           if (old && (DECL_CONTEXT (old) == current_class_type
  2823.               || ! TREE_PRIVATE (old)))
  2824.         /* Should figure out visibility more accurately.  */
  2825.         warning ("shadowing member `%s' with member function",
  2826.              IDENTIFIER_POINTER (DECL_ORIGINAL_NAME (*methods)));
  2827.           tmp = build_tree_list (DECL_ORIGINAL_NAME (*methods), *methods);
  2828.         }
  2829.  
  2830.       TREE_TYPE (tmp) = unknown_type_node;
  2831. #if 0
  2832.       TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
  2833. #endif
  2834.       TREE_NONLOCAL (tmp) = 1;
  2835.       IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods)) = tmp;
  2836.  
  2837.       tmp = *methods;
  2838.       while (tmp != 0)
  2839.         {
  2840.           TREE_FIELD_PUBLIC (tmp) = 0;
  2841.           TREE_FIELD_PROTECTED (tmp) = 0;
  2842.           TREE_FIELD_PRIVATE (tmp) = 0;
  2843.           tmp = TREE_CHAIN (tmp);
  2844.         }
  2845.  
  2846.       methods++;
  2847.     }
  2848.     }
  2849.   CLASSTYPE_MARKED (type) = 1;
  2850. }
  2851.  
  2852. /* Consolidate unique (by name) member functions.  */
  2853. static void
  2854. dfs_compress_decls (type)
  2855.      tree type;
  2856. {
  2857.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  2858.  
  2859.   if (method_vec != 0)
  2860.     {
  2861.       /* Farm out constructors and destructors.  */
  2862.       tree *methods = &TREE_VEC_ELT (method_vec, 1);
  2863.       tree *end = TREE_VEC_END (method_vec);
  2864.  
  2865.       for (; methods != end; methods++)
  2866.     {
  2867.       tree tmp = IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods));
  2868.  
  2869.       /* This was replaced in scope by somebody else.  Just leave it
  2870.          alone.  */
  2871.       if (TREE_CODE (tmp) != TREE_LIST)
  2872.         continue;
  2873.  
  2874.       if (TREE_CHAIN (tmp) == NULL_TREE
  2875.           && TREE_VALUE (tmp)
  2876.           && TREE_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
  2877.         {
  2878.           IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods))
  2879.         = TREE_VALUE (tmp);
  2880.         }
  2881.     }
  2882.     }
  2883.   CLASSTYPE_MARKED (type) = 0;
  2884. }
  2885.  
  2886. /* When entering the scope of a class, we cache all of the
  2887.    fields that that class provides within its inheritance
  2888.    lattice.  Where ambiguities result, we mark them
  2889.    with `error_mark_node' so that if they are encountered
  2890.    without explicit qualification, we can emit an error
  2891.    message.  */
  2892. void
  2893. push_class_decls (type)
  2894.      tree type;
  2895. {
  2896.   struct obstack *ambient_obstack = current_obstack;
  2897.  
  2898. #if 0
  2899.   tree tags = CLASSTYPE_TAGS (type);
  2900.  
  2901.   while (tags)
  2902.     {
  2903.       tree code_type_node;
  2904.       tree tag;
  2905.  
  2906.       switch (TREE_CODE (TREE_VALUE (tags)))
  2907.     {
  2908.     case ENUMERAL_TYPE:
  2909.       code_type_node = enum_type_node;
  2910.       break;
  2911.     case RECORD_TYPE:
  2912.       code_type_node = record_type_node;
  2913.       break;
  2914.     case CLASS_TYPE:
  2915.       code_type_node = class_type_node;
  2916.       break;
  2917.     case UNION_TYPE:
  2918.       code_type_node = union_type_node;
  2919.       break;
  2920.     default:
  2921.       assert (0);
  2922.     }
  2923.       tag = xref_tag (code_type_node, TREE_PURPOSE (tags),
  2924.               CLASSTYPE_BASECLASS (TREE_VALUE (tags), 1));
  2925.       pushdecl (build_decl (TYPE_DECL, TREE_PURPOSE (tags), TREE_VALUE (tags)));
  2926.     }
  2927. #endif
  2928.  
  2929.   current_obstack = &bridge_obstack;
  2930.   search_stack = push_search_level (search_stack, &bridge_obstack);
  2931.  
  2932.   /* Push class fields into CLASS_VALUE scope, and mark.  */
  2933.   dfs_walk (type, dfs_pushdecls, unmarkedp);
  2934.  
  2935.   /* Compress fields which have only a single entry
  2936.      by a given name, and unmark.  */
  2937.   dfs_walk (type, dfs_compress_decls, markedp);
  2938.   current_obstack = ambient_obstack;
  2939. }
  2940.  
  2941. static void
  2942. dfs_popdecls (type)
  2943.      tree type;
  2944. {
  2945.   tree fields = TYPE_FIELDS (type);
  2946.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  2947.  
  2948.   while (fields)
  2949.     {
  2950.       if (DECL_ANON_UNION_ELEM (fields))
  2951.     {
  2952.       dfs_popdecls (TREE_TYPE (fields));
  2953.     }
  2954.       else if (DECL_NAME (fields))
  2955.     IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = NULL_TREE;
  2956.       fields = TREE_CHAIN (fields);
  2957.     }
  2958.   if (method_vec != 0)
  2959.     {
  2960.       tree *methods = &TREE_VEC_ELT (method_vec, 0);
  2961.       tree *end = TREE_VEC_END (method_vec);
  2962.  
  2963.       if (*methods == 0)
  2964.     methods += 1;
  2965.  
  2966.       for (; methods != end; methods++)
  2967.     IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods)) = NULL_TREE;
  2968.     }
  2969.  
  2970.   CLASSTYPE_MARKED (type) = 1;
  2971. }
  2972.  
  2973. void
  2974. pop_class_decls (type)
  2975.      tree type;
  2976. {
  2977.   /* Clear out the IDENTIFIER_CLASS_VALUE which this
  2978.      class may have occupied, and mark.  */
  2979.   dfs_walk (type, dfs_popdecls, unmarkedp);
  2980.  
  2981.   /* Unmark.  */
  2982.   dfs_walk (type, dfs_unmark, markedp);
  2983.   search_stack = pop_search_level (search_stack);
  2984. }
  2985.  
  2986. /* Given a base type PARENT, and a derived type TYPE, build
  2987.    a name which distinguishes exactly the PARENT member of TYPE's type.
  2988.  
  2989.    FORMAT is a string which controls how sprintf formats the name
  2990.    we have generated.
  2991.  
  2992.    For example, given
  2993.  
  2994.     class A; class B; class C : A, B;
  2995.  
  2996.    it is possible to distinguish "A" from "C's A".  And given
  2997.  
  2998.     class L;
  2999.     class A : L; class B : L; class C : A, B;
  3000.  
  3001.    it is possible to distinguish "L" from "A's L", and also from
  3002.    "C's L from A".  */
  3003. tree
  3004. build_type_pathname (format, parent, type)
  3005.      char *format;
  3006.      tree parent, type;
  3007. {
  3008.   extern struct obstack temporary_obstack;
  3009.   char *first, *base, *name;
  3010.   int i;
  3011.   tree id;
  3012.  
  3013.   parent = TYPE_MAIN_VARIANT (parent);
  3014.  
  3015.   /* Remember where to cut the obstack to.  */
  3016.   first = obstack_base (&temporary_obstack);
  3017.  
  3018.   /* Put on TYPE+PARENT.  */
  3019.   obstack_grow (&temporary_obstack,
  3020.         TYPE_NAME_STRING (type), TYPE_NAME_LENGTH (type));
  3021.   obstack_1grow (&temporary_obstack, JOINER);
  3022.   obstack_grow0 (&temporary_obstack,
  3023.          TYPE_NAME_STRING (parent), TYPE_NAME_LENGTH (parent));
  3024.   i = obstack_object_size (&temporary_obstack);
  3025.   base = obstack_base (&temporary_obstack);
  3026.   obstack_finish (&temporary_obstack);
  3027.  
  3028.   /* Put on FORMAT+TYPE+PARENT.  */
  3029.   obstack_blank (&temporary_obstack, strlen (format) + i + 1);
  3030.   name = obstack_base (&temporary_obstack);
  3031.   sprintf (name, format, base);
  3032.   id = get_identifier (name);
  3033.   obstack_free (&temporary_obstack, first);
  3034.  
  3035.   return id;
  3036. }
  3037.  
  3038. static int
  3039. bfs_unmark_finished_struct (type, i)
  3040.      tree type;
  3041.      int i;
  3042. {
  3043.   type = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  3044.   if (CLASSTYPE_MARKED4 (type))
  3045.     {
  3046.       tree assoc, decl;
  3047.  
  3048.       if (type == current_class_type)
  3049.     assoc = CLASSTYPE_ASSOC (type);
  3050.       else if (TREE_VIA_VIRTUAL (type))
  3051.     assoc = value_member (TYPE_MAIN_VARIANT (type), CLASSTYPE_VBASECLASSES (current_class_type));
  3052.       else
  3053.     assoc = assoc_value (TYPE_MAIN_VARIANT (type), current_class_type);
  3054.       decl = ASSOC_VTABLE (assoc);
  3055.       if (write_virtuals >= 0
  3056.       && DECL_INITIAL (decl) != ASSOC_VIRTUALS (assoc))
  3057.     DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
  3058.                     ASSOC_VIRTUALS (assoc));
  3059.       finish_decl (decl, DECL_INITIAL (decl), NULL_TREE);
  3060.     }
  3061.   CLASSTYPE_MARKED3 (type) = 0;
  3062.   CLASSTYPE_MARKED4 (type) = 0;
  3063.   return 0;
  3064. }
  3065.  
  3066. void
  3067. unmark_finished_struct (type)
  3068.      tree type;
  3069. {
  3070.   bfs_unmark_finished_struct (type, 0);
  3071.   breadth_first_search (type, bfs_unmark_finished_struct, bfs_marked3p);
  3072. }
  3073.  
  3074. void
  3075. print_search_statistics ()
  3076. {
  3077. #ifdef GATHER_STATISTICS
  3078.   if (flag_memoize_lookups)
  3079.     {
  3080.       fprintf (stderr, "%d memoized contexts saved\n",
  3081.            n_contexts_saved);
  3082.       fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
  3083.       fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
  3084.       fprintf (stderr, "fields statistics:\n");
  3085.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3086.            memoized_fast_finds[0], memoized_fast_rejects[0],
  3087.            memoized_fields_searched[0]);
  3088.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[0]);
  3089.       fprintf (stderr, "fnfields statistics:\n");
  3090.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3091.            memoized_fast_finds[1], memoized_fast_rejects[1],
  3092.            memoized_fields_searched[1]);
  3093.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[1]);
  3094.     }
  3095.   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
  3096.        n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
  3097.   fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
  3098.        n_outer_fields_searched, n_calls_lookup_fnfields);
  3099.   fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
  3100. #else
  3101.   fprintf (stderr, "no search statistics\n");
  3102. #endif
  3103. }
  3104.  
  3105. void
  3106. init_search_processing ()
  3107. {
  3108.   obstack_init (&search_obstack);
  3109.   obstack_init (&type_obstack);
  3110.   obstack_init (&type_obstack_entries);
  3111.   obstack_init (&bridge_obstack);
  3112.  
  3113.   /* This gives us room to build our chains of basetypes,
  3114.      whether or not we decide to memoize them.  */
  3115.   type_stack = push_type_level (0, &type_obstack);
  3116.   _vptr_name = get_identifier ("_vptr");
  3117. }
  3118.  
  3119. tree
  3120. get_wrapper (type)
  3121.      tree type;
  3122. {
  3123.   tree wrap_type;
  3124.   char *name;
  3125.   assert (IS_AGGR_TYPE (type));
  3126.   wrap_type = TYPE_WRAP_TYPE (type);
  3127.   name = (char *)alloca (TYPE_NAME_LENGTH (wrap_type)
  3128.              + strlen (WRAPPER_NAME_FORMAT));
  3129.   sprintf (name, WRAPPER_NAME_FORMAT, TYPE_NAME_STRING (wrap_type));
  3130.   return lookup_fnfields (CLASSTYPE_AS_LIST (wrap_type),
  3131.               get_identifier (name), 0);
  3132. }
  3133.  
  3134. void
  3135. reinit_search_statistics ()
  3136. {
  3137.   my_memoized_entry_counter = 0;
  3138.   memoized_fast_finds[0] = 0;
  3139.   memoized_fast_finds[1] = 0;
  3140.   memoized_adds[0] = 0;
  3141.   memoized_adds[1] = 0;
  3142.   memoized_fast_rejects[0] = 0;
  3143.   memoized_fast_rejects[1] = 0;
  3144.   memoized_fields_searched[0] = 0;
  3145.   memoized_fields_searched[1] = 0;
  3146.   n_fields_searched = 0;
  3147.   n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
  3148.   n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
  3149.   n_calls_get_base_type = 0;
  3150.   n_outer_fields_searched = 0;
  3151.   n_contexts_saved = 0;
  3152. }
  3153.